void CPaletteDlg::RGBtoHSL(BYTE r, BYTE g, BYTE b, UINT &hue, UINT &sat, UINT &light)
//RGB-->HSLR的颜色转换;
{
double v=255.0;
double red = r / 255.0, green = g / 255.0, blue = b / 255.0;
double h, s, l, maxCol, minCol, difCol, sumCol;
maxCol = max(red, max(green, blue));
minCol = min(red, min(green, blue));
difCol = maxCol - minCol;
sumCol = maxCol + minCol;
l = sumCol / 2;
if (maxCol == minCol) s = h = 0;
else {
if (l < 0.5) s = difCol / sumCol;
else s = difCol / (2 - sumCol);
if (red == maxCol) h = (green - blue) / difCol;
else if (green == maxCol) h = 2 + (blue - red) / difCol;
else if (blue == maxCol) h = 4 + (red - green) / difCol;
}
int hi = (int)(h * 60 + 0.5);
if (hi < 0) hi += 360; else if (hi > 360) hi %= 360;
hue = hi;
sat = (int)(s * v + 0.5);
light = (int)(l * v + 0.5);
}
void CPaletteDlg::HSLtoRGB(UINT hue, UINT sat, UINT light, BYTE &r, BYTE &g, BYTE &b)
//HSL---->RGB的颜色转换;
{
double v=255.0;
double h = hue / 360.0, s = sat / v, l = light / v;
double red, green, blue;
double t1, t2, tr, tg, tb;
static const double v13 = 1 / 3.0;
if (s == 0) red = green = blue = l;
else {
if (l < 0.5) t2 = l * (1 + s);
else t2 = l + s - l * s;
t1 = 2 * l - t2;
tr = h + v13; if (tr > 1) tr -= 1;
tg = h;
tb = h - v13; if (tb < 0) tb += 1;
red = rgb(t1, t2, tr);
green = rgb(t1, t2, tg);
blue = rgb(t1, t2, tb);
}
r = (BYTE)(red * 255 + 0.5);
g = (BYTE)(green * 255 + 0.5);
b = (BYTE)(blue * 255 + 0.5);
}