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

By , 17 Aug 2005
 

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

About the Author

TTRider
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralInfinite hue shift [modified]memberSaso Tepina15 Jan '09 - 6:12 
GeneralWrong Brighteningmembermephistok15 Oct '07 - 2:47 
AnswerRe: Wrong Brighteningmemberdlk1322 Feb '08 - 6:49 
QuestionHow to contact the author?memberbl.nero14 Jun '06 - 0:08 
GeneralAnother ClassmemberGeeky Monkey24 Aug '05 - 9:11 
GeneralReminds me of Bob Powell's worksussAnonymous23 Aug '05 - 21:16 
GeneralFurther reading...memberDrew Stainton18 Aug '05 - 7:13 
GeneralComparing colormemberStephane David17 Aug '05 - 21:43 
GeneralRe: Comparing colormemberDrew Stainton18 Aug '05 - 7:24 
GeneralRe: Comparing colormemberphilippe dykmans11 Nov '05 - 5:15 
GeneralinterestingmemberAshley van Gerven17 Aug '05 - 14:30 
GeneralRe: interestingmemberTTRider17 Aug '05 - 14:38 
GeneralRe: interestingmembersabrown10025 Oct '07 - 13:18 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 17 Aug 2005
Article Copyright 2005 by TTRider
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid