RGB, HSL and HSV
Site Map Feedback

Download:

Up Colour Spline HSL HSV Mixers Shaders

If you got to this page looking for a simple HSL-RGB converter that returns BYTE components as used by Windows, you may prefer to use the HSL-RGB Converter.

Saturation gColor is designed for fast, accurate colour manipulation. Each colour component (R,G,B,A) is held as a double in the interval [0,1]. Each component can be set or retrieved as float or double with this interval as well as BYTE or int in the interval [0,255]. Constructors exist to create a colour from the components, as well as from the Hue (pick a colour from the spectrum in the interval [0,1]), a 'NoColor' (useful when a system has a 'default colour'), or a random colour. Saturation can be set using Perceived Luminance in its calculation (see image). Brightness is adjustable and some static methods provide Gamma Correction, Exposure and Light Power methods. The RGBA components can be set or retrieved as HSL or HSV components.


HSL and HSV

H is the Hue (colour); S is the Saturation (greyness) and L is the Lightness (luminance), V is the 'Value'. Lightness is the middle of the range of component values and will alter the colour right through to white; Value is that of the maximum component and won't reach white unless all components are equal; so if the [R,G,B] values are [0.2,0.6,0.5] then L=(0.2+0.6)/2 and V=0.6
Lightness Value
These are generally used for user-interfaces, for picking colour shades. HSL is recommended for user interfaces (varying the saturation HSL is the most intuitive); HSV is a faster algorithm to calculate and is used in the colour mixing methods. The HSL and HSV methods both return components as doubles in the interval [0,1], so you can easily re-map them to any values you need. To map from Windows' HSL components which are in the range [0,240], you can use:

  BYTE H=From Windows...;
  BYTE S=From Windows...;
  BYTE L=From Windows...;
  gColor Col;
  Col.SetFromHSL(H/240.0, S/240.0, L/240.0);
  BYTE R=Col.GetRb();
  BYTE G=Col.GetGb();
  BYTE B=Col.GetBb();

'Brightness'

Perceived Luminance can be calculated and the 'Brightness' can be set (which also alters the hue). The code to generate the images follows:

'Brightness'
  gColor Color1;
  for(WORD x=GetWidth(); x--;) {
    double t=double(x)/GetWidth();
    Color1.SetFromHue(t);
    for(WORD y=GetHeight(); y--;) {
      double s=double(y)/GetHeight();
      gColor Color2(Color1);
      Color2.SetBrightness(s);
      SetPixel(x,y, Color2.GetRGB());
  } }

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


 (2325) Last modified: Wed, 03 Jun 2009 09:35:18 +0000