Click here to Skip to main content
Click here to Skip to main content

Use both RGB and HSB color schemas in your .NET application using HSBColor class

, 17 Aug 2005
Rate this:
Please Sign up or sign in to vote.
This class helps to convert RGB colors to HSB and back.

Introduction

.NET Framework provides implementation of the Color class and several satellite classes like KnownColor and SystemColors. But all of them are based on ARGB schema. And this is logical, since Windows itself is based on it. But in many cases ARGB is not very useful. For example, you want to use a color that is slightly brighter than a specific system color. For this kind of manipulation you need a different color coding, like HSB (Hue-Saturation-Brightness).

System.Color class provides us these values, but unfortunately it can’t be recreated using them. The purpose of the class I’m going to present in this article is to provide an ability to convert from RGB to HSB and back.

Here is the signature of the class:

using System;

namespace TTRider.UI
{
    public struct HSBColor
    {
        public HSBColor(System.Drawing.Color color);
        
        public HSBColor(float h, float s, float b);
        
        public HSBColor(int a, float h, float s, float b);
        
        public int A { get; }
        
        public float H { get; }
        
        public float S { get; }
        
        public float B { get; }
        
        public System.Drawing.Color Color { get; }
        
        
        public static HSBColor FromColor(System.Drawing.Color color);
        
        public static System.Drawing.Color FromHSB(HSBColor hsbColor);
        
        public static System.Drawing.Color ShiftBrighness(
                       System.Drawing.Color c, float brightnessDelta);
        
        public static System.Drawing.Color ShiftHue(
                              System.Drawing.Color c, float hueDelta);
        
        public static System.Drawing.Color ShiftSaturation(
                       System.Drawing.Color c, float saturationDelta);
    }
}

As you can see, you can create an instance of this struct using System.Color class or just use the Hue, Saturation and Brightness values.

Be aware! You can't use these values from the Color class. Looks like .NET uses different conversion formulas. The valid range of each component is 0:255. Looks like System.Color uses the range from 0:1 for Brightness and Saturation. There are several helpful static members like ShiftHue, ShiftSaturation and ShiftBrightness to manipulate your System.Color class.

You can look into the implementation of this class in the source files. It is straightforward.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

TTRider
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralInfinite hue shift [modified] PinmemberSaso Tepina15-Jan-09 7:12 
GeneralWrong Brightening Pinmembermephistok15-Oct-07 3:47 
AnswerRe: Wrong Brightening Pinmemberdlk1322-Feb-08 7:49 
QuestionHow to contact the author? Pinmemberbl.nero14-Jun-06 1:08 
GeneralAnother Class PinmemberGeeky Monkey24-Aug-05 10:11 
GeneralReminds me of Bob Powell's work PinsussAnonymous23-Aug-05 22:16 
GeneralFurther reading... PinmemberDrew Stainton18-Aug-05 8:13 
GeneralComparing color PinmemberStephane David17-Aug-05 22:43 
GeneralRe: Comparing color PinmemberDrew Stainton18-Aug-05 8:24 
GeneralRe: Comparing color Pinmemberphilippe dykmans11-Nov-05 6:15 
Hi,
 
I agree with Drew Stainton. The human eye has flaws that make it perceive colour in unexpected ways. And this makes "natural" automatic colour matching difficult. But there is one approach that is at least "mathematically" correct.
 
Look at the three colours R, G and B as axes of a 3D space. You could then express the proximity of colours as their mathimatical (Euclidian) distance in this space. Given two colours with respective colour components r1, r2, g1, g2, b1, b2 you would have to calculate:
 
sqrt((r1 - r2)^2 + ((g1 - g2)^2 + (b1 - b2)^2)
 
where ^ means power to.
 
Applying this function 'out of the box' will yield unexpected results. Like one type of brown will be mathematically closer to some distinct purple than to another brown that our eye perceives as an almost identical brown.
 
My guess is that you should apply a certain weight to every difference factor in the equation to yield more 'natural' results. Maybe start with the relative sensitivity of the human eye for red, green and blue. You can find starting values for those on the net. I'm sure.
 
Good luck!
Phil
 
Philippe Dykmans
Software developpement
University of Antwerp
Generalinteresting PinmemberAshley van Gerven17-Aug-05 15:30 
GeneralRe: interesting PinmemberTTRider17-Aug-05 15:38 
GeneralRe: interesting Pinmembersabrown10025-Oct-07 14:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 17 Aug 2005
Article Copyright 2005 by TTRider
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid