C++ RGB转HSL HSL转RGB
作者:互联网
//Int 转ARGB
void CGamma::IntToARgb(int *i, BYTE &byA, BYTE &byR, BYTE &byG, BYTE &byB)
{
byB = (BYTE)(0xff & *i);
byG = (BYTE)((0xff00 & *i) >> 8);
byR = (BYTE)((0xff0000 & *i) >> 16);
byA = (BYTE)((0xff000000 & *i) >> 24);
}
//RGB转HSL
void CGamma::RGBToHSL(int nColor, double &H, double &S, double &L)
{
double r = (0xff0000 & nColor) >> 16;
double g = (0xff00 & nColor) >> 8;
double b = 0xff & nColor;
r = r / 255;
g = g / 255;
b = b / 255;
double M = max(max(r, g), b);
double m = min(min(r, g), b);
double d = M - m;
if (d == 0)
{
H = 0;
}
else if (M == r)
{
H = ((g - b) / d);
H = H + ((int)H % 6);
}
else if (M == g)
{
H = (b - r) / d + 2;
}
else
{
H = (r - g) / d + 4;
}
H *= 60;
H = (H < 0) ? H += 360 : H;
L = (M + m) / 2;
S = (d == 0) ? S = 0 : d / (1 - abs(2 * L - 1)); //如果放大或者缩小 乘以或者除以相应的倍数
S = S * 255;
L = L * 255;
}
//HSL转RGB
int CGamma::HSLToRGB(double dHue, double dSaturation, double dLightness)
{
dSaturation = dSaturation / 255.0;
dLightness = dLightness / 255.0;
double C = (1 - abs(2 * dLightness - 1)) * dSaturation;
double hh = dHue / 60.0;
double xiaoshu = hh - long(hh);
double X = C * (1 - abs((int)hh % 2 + xiaoshu - 1));
double r = 0, g = 0, b = 0;
if (hh >= 0 && hh < 1)
{
r = C;
g = X;
}
else if (hh >= 1 && hh < 2)
{
r = X;
g = C;
}
else if (hh >= 2 && hh < 3)
{
g = C;
b = X;
}
else if (hh >= 3 && hh < 4)
{
g = X;
b = C;
}
else if (hh >= 4 && hh < 5)
{
r = X;
b = C;
}
else
{
r = C;
b = X;
}
double m = dLightness - C / 2;
r = (int)(((r+m) *255) + 0.5);
g = (int)(((g + m) * 255) + 0.5);
b = (int)(((b + m) * 255) + 0.5);
int addr = (int)b & 0xFF;
addr |= ((int)g << 8) & 0xFF00;
addr |= ((int)r << 16) & 0xFF0000;
return addr;
}
标签:int,double,hh,C++,else,RGB,HSL,BYTE,255 来源: https://blog.csdn.net/immotal_xiaoqiang/article/details/122622474