如何计算两种颜色之间的差异(亮度,亮度,颜色)?
问题描述:
- 亮度对比度,
- 的亮度差,和
- 的差在颜色
两个给定的颜色之间?
实施例:
输入
- 颜色1:#99CCFF
- 颜色2:#225588
输出
- 光度对比度:4.57:1的亮度
- 差:在颜色119
- 差:357
答
从转换各颜色(R,G,B)到(H,S, V)[或(H,S,L)]坐标。你可以找到公式at Wikipedia。
我已经写帕斯卡尔功能如下:
function RGBToHSV(const Color: TRGB): THSV;
var
cmax, cmin, cdiff: real;
begin
cmax := MaxComponent(Color);
cmin := MinComponent(Color);
cdiff := cmax - cmin;
with Color, result do
begin
// Hue
if cmax = cmin then
hsvHue := 0
else if cmax = rgbRed then
hsvHue := (60 * (rgbGreen - rgbBlue)/cdiff)
else if cmax = rgbGreen then
hsvHue := (60 * (rgbBlue - rgbRed)/cdiff) + 120
else
hsvHue := (60 * (rgbRed - rgbGreen)/cdiff) + 240;
hsvHue := Fix360(hsvHue);
// Saturation
if cmax = 0 then
hsvSaturation := 0
else
hsvSaturation := 1 - cmin/cmax;
// Value
hsvValue := cmax;
end;
end;
function RGBToHSL(const Color: TRGB): THSL;
var
cmax, cmin, cdiff, csum: real;
begin
cmax := MaxComponent(Color);
cmin := MinComponent(Color);
cdiff := cmax - cmin;
csum := cmax + cmin;
with Color, result do
begin
// Hue
if cmax = cmin then
hslHue := 0
else if cmax = rgbRed then
hslHue := (60 * (rgbGreen - rgbBlue)/cdiff)
else if cmax = rgbGreen then
hslHue := (60 * (rgbBlue - rgbRed)/cdiff) + 120
else
hslHue := (60 * (rgbRed - rgbGreen)/cdiff) + 240;
hslHue := Fix360(hslHue);
// Saturation
if cmax = cmin then
hslSaturation := 0
else if csum <= 1 then
hslSaturation := cdiff/csum
else
hslSaturation := cdiff/(2 - csum);
// Lightness
hslLightness := csum/2;
end;
end;
我好像记得是这样的((red_value1 * 299 + green_value1 * 587 + blue_value1 * 114)/(red_value2 * 299 + green_value2 * 587 + blue_value2 * 114)) – 2011-06-10 14:51:01
@ anirudh4444:对于我的示例,结果是2.57490315 ...看起来不像任何预期的输出。这应该算什么?任何想法,这些神奇的数字来自哪里? – Shawn 2011-06-10 15:14:54