我正在尝试从我的数据库中获取HSL颜色值。目前仅存储RGB值。假设我为rgb提供了单独的列:红绿蓝,每个都有一个数值0-255。
目标结果将是结果集中的色调饱和度亮度,根据rgb值计算。我已经看过很多计算但是在查询中它们似乎都不容易做到?或者我一般都不熟悉SQL,知道如何将类似switch语句移植到SQL。
我发现转换的最佳示例是:How do you get the hue of a #xxxxxx colour?
function rgbToHsl(r, g, b){
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if(max == min){
h = s = 0; // achromatic
}else{
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h, s, l];
}
但是我完全不知道如何在SQL中做这样的计算。
在它工作不正常之后(上面的代码示例不正确,我可以在维基百科上看到的转换色调,以及我需要完全度0-360而不是0到1之间的色调)我从使用Arth的解决方案开始,我已经决定事先在r,g,b上使用/ 255,因为从上面的代码示例中可以更容易地遵循:
CASE
WHEN r>=g AND g>=b THEN ((g-b)/(r-b))*60
WHEN g>r AND r>=b THEN (2-(r-b)/(g-b))*60
WHEN g>=b AND b>r THEN (2+(b-r)/(g-r))*60
WHEN b>g AND g>r THEN (4-(g-r)/(b-r))*60
WHEN b>r AND r>=g THEN (4+(r-g)/(b-g))*60
WHEN r>=b AND b>g THEN (6-(b-g)/(r-g))*60
END h,
CASE
WHEN r=g AND g=b THEN 0
WHEN r>=g AND g>=b AND (r-b)>0.5 THEN (r-b)/(2-r-b)
WHEN r>=g AND g>=b THEN (r-b)/(r+b)
WHEN r>=g AND b>g AND (r-g)>0.5 THEN (r-g)/(2-r-g)
WHEN r>=g AND b>g THEN (r-g)/(r+g)
WHEN g>=r AND r>=b AND (g-b)>0.5 THEN (g-b)/(2-g-b)
WHEN g>=r AND r>=b THEN (g-b)/(g+b)
WHEN g>=r AND b>r AND (g-r)>0.5 THEN (g-r)/(2-g-r)
WHEN g>=r AND b>r THEN (g-r)/(g+r)
WHEN b>=r AND r>=g AND (b-g)>0.5 THEN (b-g)/(2-b-g)
WHEN b>=r AND r>=g THEN (b-g)/(b+g)
WHEN b>=r AND g>r AND (b-r)>0.5 THEN (b-r)/(2-b-r)
WHEN b>=r AND g>r THEN (b-r)/(b+r)
END s,
CASE
WHEN r=g AND g=b THEN r
WHEN r>=g AND g>=b THEN (r+b)/2
WHEN r>=g AND b>g THEN (r+g)/2
WHEN g>=r AND r>=b THEN (g+r)/2
WHEN g>=r AND b>r THEN (g+b)/2
WHEN b>=r AND r>=g THEN (b+g)/2
WHEN b>=r AND g>r THEN (b+r)/2
END l
答案 0 :(得分:2)
这绝对是一场噩梦而未经过测试,但我已经开始了:
SELECT
CASE
WHEN r=g AND g=b THEN 0
WHEN r>=g AND g>b THEN ((g-b)/(r-b))/6
WHEN r>=g AND b>=g THEN ((g-b)/(r-g)+6)/6
WHEN g>=r AND r>=b THEN ((b-r)/(g-b)+2)/6
WHEN g>=r AND b>r THEN ((b-r)/(g-r)+2)/6
WHEN b>=r AND r>=g THEN ((r-g)/(b-g)+4)/6
WHEN b>=r AND g>r THEN ((r-g)/(b-r)+4)/6
END h,
CASE
WHEN r=g AND g=b THEN 0
WHEN r>=g AND g>=b AND (r-b)>0.5*255 THEN (r-b)/(510-r-b)
WHEN r>=g AND g>=b THEN (r-b)/(r+b)
WHEN r>=g AND b>g AND (r-g)>0.5*255 THEN (r-g)/(510-r-g)
WHEN r>=g AND b>g THEN (r-g)/(r+g)
WHEN g>=r AND r>=b AND (g-b)>0.5*255 THEN (g-b)/(510-g-b)
WHEN g>=r AND r>=b THEN (g-b)/(g+b)
WHEN g>=r AND b>r AND (g-r)>0.5*255 THEN (g-r)/(510-g-r)
WHEN g>=r AND b>r THEN (g-r)/(g+r)
WHEN b>=r AND r>=g AND (b-g)>0.5*255 THEN (b-g)/(510-b-g)
WHEN b>=r AND r>=g THEN (b-g)/(b+g)
WHEN b>=r AND g>r AND (b-r)>0.5*255 THEN (b-r)/(510-b-r)
WHEN b>=r AND g>r THEN (b-r)/(b+r)
END s,
CASE
WHEN r=g AND g=b THEN r/255
WHEN r>=g AND g>=b THEN (r+b)/510
WHEN r>=g AND b>g THEN (r+g)/510
WHEN g>=r AND r>=b THEN (g+r)/510
WHEN g>=r AND b>r THEN (g+b)/510
WHEN b>=r AND r>=g THEN (b+g)/510
WHEN b>=r AND g>r THEN (b+r)/510
END l
FROM table1
总之,您最好选择RGB值并在应用程序级别运行转换!