Mutual conversion of RGB and HSL colors

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/aniven/article/details/2205851

RGB and HSL (also called HSB/HSV) are two color spaces, namely: red, green, Blue (red,green,blue) and hue, saturation, brightness (Hue,saturation,lightness or brightness or value), the former is suitable for machine sampling, the current display color is composed of these three colors, and the latter more in line with human visual sense, such as people generally express a color will say: a little thick dark red. Not to say how much red, green accounted for how much, blue accounted for.
Both of these representations are included in the standard Color dialog box for Windows. The RGB value range is between 0~255, HSL's value is between 0~1, but the Windows system is processed into a range of 0~240 values, the values in various environments refer to the schedule. In addition there are cmy/cmyk color space, often used in the printing industry, and then the Rgb-cmy-cmyk conversion algorithm posted.

Convert RGB to HSL:
#include <algorithm>
Using Std::min;
Using Std::max;

void RGB2HSL (TColor acolor, double &h,double &s,double &l)
{
Double R,g,b,max,min,del_r,del_g,del_b,del_max;
R = Getrvalue (acolor)/255.0; Where RGB values = 0÷255
G = Getgvalue (acolor)/255.0;
B = Getbvalue (acolor)/255.0;

min = min (R, min (G, B)); Min. Value of RGB
max = max (R, Max (G, B)); Max. Value of RGB
Del_max = Max-min; Delta RGB Value

L = (Max + Min)/2.0;

if (Del_max = = 0)//this is a gray, no chroma ...
{
H = 2.0/3.0; Under Windows S value 0 o'clock, h value is always 160 (2/3*240)
H = 0; HSL results = 0÷1
S = 0;
}
Else//chromatic data ...
{
if (L < 0.5) S = Del_max/(Max + Min);
else S = Del_max/(2-max-min);

Del_r = (((max-r)/6.0) + (del_max/2.0))/Del_max;
Del_g = (((max-g)/6.0) + (del_max/2.0))/Del_max;
Del_b = (((max-b)/6.0) + (del_max/2.0))/Del_max;

if (R = = Max) H = Del_b-del_g;
else if (G = = Max) H = (1.0/3.0) + del_r-del_b;
else if (B = = Max) H = (2.0/3.0) + Del_g-del_r;

if (H < 0) H + = 1;
if (H > 1) H-= 1;
}
}

HSL conversion to RGB:
TColor Hsl2rgb (double h,double s,double L)
{
Double r,g,b;
Double var_1, var_2;
if (S = = 0)//HSL values = 0÷1
{
R = L * 255.0; RGB results = 0÷255
G = L * 255.0;
B = L * 255.0;
}
Else
{
if (L < 0.5) var_2 = L * (1 + S);
else var_2 = (L + s)-(S * l);

var_1 = 2.0 * l-var_2;

R = 255.0 * HUE2RGB (var_1, var_2, H + (1.0/3.0));
G = 255.0 * HUE2RGB (var_1, var_2, H);
B = 255.0 * HUE2RGB (var_1, var_2, H-(1.0/3.0));
}
Return TColor (RGB (r,g,b));
}
//---------------------------------------------------------------------------
Double Hue2rgb (Double v1, double v2, double VH)
{
if (VH < 0) VH + = 1;
if (VH > 1) VH-= 1;
if (6.0 * VH < 1) return v1 + (V2-V1) * 6.0 * VH;
if (2.0 * VH < 1) return v2;
if (3.0 * VH < 2) return v1 + (V2-V1) * ((2.0/3.0)-VH) * 6.0;
return (v1);
}

If you want to get the HSL value in Windows, you can overload these two functions and change the arguments to the int type:
#include <Math.hpp>

Convert RGB to HSL:
void RGB2HSL (TColor acolor, int &h,int &s,int &l)
{
Double h,s,l;
RGB2HSL (acolor,h,s,l);
h = roundto (h * 240,0);
s = roundto (S * 240,0);
L = roundto (L * 240,0);
}

HSL conversion to RGB:
TColor Hsl2rgb (int H, int S, int L)
{
Double h,s,l;
h = h/240.0;
s = s/240.0;
L = l/240.0;
Return Hsl2rgb (h,s,l);
}

Schedule (hsl/v/b in various environments):

applications

< P align= "Center" >space

H  range

S  range

l/v/b  range

Paint shop Pro

HSL

0-255

0-255

L

0-255

Gimp

< P align= "Center" >HSV

< Span style= "Font-size:small;" >0-360°

0-100

V

0-100

Photoshop

HSV

0-360°

0-100%

B

0-100%

Windows

HSL

0-240

0-240

L

0-240

Linux/kde

HSV

0-360°

0-255

V

GTK

< P align= "Center" >HSV

< Span style= "Font-size:small;" >0-360°

0-1.0

V

0-1.0

Java (AWT. Color)

HSV

0-1.0

0-1.0

B

0-1.0

Apple

Hsv

0-360°

0-100%

L

0-100%

Mutual conversion of RGB and HSL colors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.