我正在寻找某种公式或算法来确定给定RGB值的颜色的亮度。我知道这不像把RGB值加在一起那么简单,更高的总和更亮,但我有点不知所措,不知道从哪里开始。
当前回答
有趣的是,RGB=>HSV的公式只是使用v=MAX3(r,g,b)。换句话说,你可以用(r,g,b)的最大值作为HSV中的V。
我查了一下,在Hearn & Baker的575页,这也是他们计算“价值”的方法。
其他回答
为了清晰起见,使用平方根的公式必须是
√(系数* (colour_value^2))
not
√(系数*颜色值)^2
证明这一点的证据在于将R=G=B三位一体转换为灰度R。只有当你将颜色值平方,而不是颜色值乘以系数时,这才成立。参见灰色的九种色调
这里有一小段C代码,可以正确地计算可感知的亮度。
// reverses the rgb gamma
#define inverseGamma(t) (((t) <= 0.0404482362771076) ? ((t)/12.92) : pow(((t) + 0.055)/1.055, 2.4))
//CIE L*a*b* f function (used to convert XYZ to L*a*b*) http://en.wikipedia.org/wiki/Lab_color_space
#define LABF(t) ((t >= 8.85645167903563082e-3) ? powf(t,0.333333333333333) : (841.0/108.0)*(t) + (4.0/29.0))
float
rgbToCIEL(PIXEL p)
{
float y;
float r=p.r/255.0;
float g=p.g/255.0;
float b=p.b/255.0;
r=inverseGamma(r);
g=inverseGamma(g);
b=inverseGamma(b);
//Observer = 2°, Illuminant = D65
y = 0.2125862307855955516*r + 0.7151703037034108499*g + 0.07220049864333622685*b;
// At this point we've done RGBtoXYZ now do XYZ to Lab
// y /= WHITEPOINT_Y; The white point for y in D65 is 1.0
y = LABF(y);
/* This is the "normal conversion which produces values scaled to 100
Lab.L = 116.0*y - 16.0;
*/
return(1.16*y - 0.16); // return values for 0.0 >=L <=1.0
}
“接受”的答案是不正确和不完整的
唯一准确的答案是@ ji- dadson和@EddingtonsMonkey的答案,并支持@ niles -pipenbrinck。其他答案(包括已接受的答案)链接到或引用了错误的、不相关的、过时的或坏的来源。
简要:
sRGB必须在应用系数之前线性化。 亮度(L或Y)与光一样是线性的。 感知亮度(L*)与人类感知一样是非线性的。 HSV和HSL在感知方面甚至远不准确。 sRGB的IEC标准指定阈值为0.04045,而不是0.03928(这是来自过时的早期草案)。 为了有用(即相对于感知),欧几里得距离需要一个感知一致的笛卡尔向量空间,如CIELAB。sRGB不是其中之一。
以下是正确而完整的回答:
由于这条线索在搜索引擎中出现频率很高,我添加了这个答案来澄清关于这个主题的各种误解。
亮度是光的线性测量,对正常视力进行光谱加权,但对亮度的非线性感知不进行调整。它可以是相对度量,如CIEXYZ中的Y,或L, cd/m2的绝对度量(不要与L*混淆)。
一些视觉模型如CIELAB使用感知明度,这里L* (Lstar)为感知明度值,且为非线性,以近似人类视觉非线性响应曲线。(也就是说,对知觉是线性的,但因此对光是非线性的)。
亮度是一种感知属性,它不具有“物理”度量。然而,一些颜色外观模型确实有一个值,通常用“Q”表示感知亮度,这与感知亮度不同。
Luma (Y´')是一种伽玛编码的加权信号,用于某些视频编码(Y´I´Q´)。不要与线性亮度混淆。
Gamma或传递曲线(TRC)是一种通常与感知曲线相似的曲线,通常用于存储或广播图像数据,以减少感知噪声和/或提高数据利用率(以及相关原因)。
为了确定感知亮度,首先将gamma编码的R´G´B´图像值转换为线性亮度(L或Y),然后转换为非线性感知亮度(L*)
寻找亮度:
...因为很明显它在某个地方丢失了……
第一步:
将所有sRGB 8位整数值转换为十进制0.0-1.0
vR = sR / 255;
vG = sG / 255;
vB = sB / 255;
第二步:
将gamma编码的RGB转换为线性值。例如,sRGB(计算机标准)要求功率曲线约为V^2.2,尽管“准确的”变换是:
其中V´为sRGB的伽玛编码R、G或B通道。 伪代码:
function sRGBtoLin(colorChannel) {
// Send this function a decimal sRGB gamma encoded color value
// between 0.0 and 1.0, and it returns a linearized value.
if ( colorChannel <= 0.04045 ) {
return colorChannel / 12.92;
} else {
return pow((( colorChannel + 0.055)/1.055),2.4);
}
}
第三步:
要找到亮度(Y),应用sRGB的标准系数:
使用上述函数的伪代码:
Y = (0.2126 * sRGBtoLin(vR) + 0.7152 * sRGBtoLin(vG) + 0.0722 * sRGBtoLin(vB))
找到可感知的轻盈:
步骤四:
从上面取亮度Y,变换为L*
伪代码:
function YtoLstar(Y) {
// Send this function a luminance value between 0.0 and 1.0,
// and it returns L* which is "perceptual lightness"
if ( Y <= (216/24389)) { // The CIE standard states 0.008856 but 216/24389 is the intent for 0.008856451679036
return Y * (24389/27); // The CIE standard states 903.3, but 24389/27 is the intent, making 903.296296296296296
} else {
return pow(Y,(1/3)) * 116 - 16;
}
}
L*是一个从0(黑色)到100(白色)的值,其中50是感知的“中间灰色”。L* = 50相当于Y = 18.4,换句话说,一张18%的灰卡,代表一张照片曝光的中间(安塞尔·亚当斯V区)。
引用:
IEC 61966-2-1:1999标准 维基百科sRGB 维基百科CIELAB 维基百科CIEXYZ Charles Poynton的Gamma常见问题解答
今天我用javascript解决了一个类似的任务。 我已经确定了这个getPerceivedLightness(rgb)函数的HEX rgb颜色。 利用Fairchild和Perrotta公式对Helmholtz-Kohlrausch效应进行了亮度校正。
/**
* Converts RGB color to CIE 1931 XYZ color space.
* https://www.image-engineering.de/library/technotes/958-how-to-convert-between-srgb-and-ciexyz
* @param {string} hex
* @return {number[]}
*/
export function rgbToXyz(hex) {
const [r, g, b] = hexToRgb(hex).map(_ => _ / 255).map(sRGBtoLinearRGB)
const X = 0.4124 * r + 0.3576 * g + 0.1805 * b
const Y = 0.2126 * r + 0.7152 * g + 0.0722 * b
const Z = 0.0193 * r + 0.1192 * g + 0.9505 * b
// For some reason, X, Y and Z are multiplied by 100.
return [X, Y, Z].map(_ => _ * 100)
}
/**
* Undoes gamma-correction from an RGB-encoded color.
* https://en.wikipedia.org/wiki/SRGB#Specification_of_the_transformation
* https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
* @param {number}
* @return {number}
*/
function sRGBtoLinearRGB(color) {
// Send this function a decimal sRGB gamma encoded color value
// between 0.0 and 1.0, and it returns a linearized value.
if (color <= 0.04045) {
return color / 12.92
} else {
return Math.pow((color + 0.055) / 1.055, 2.4)
}
}
/**
* Converts hex color to RGB.
* https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
* @param {string} hex
* @return {number[]} [rgb]
*/
function hexToRgb(hex) {
const match = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
if (match) {
match.shift()
return match.map(_ => parseInt(_, 16))
}
}
/**
* Converts CIE 1931 XYZ colors to CIE L*a*b*.
* The conversion formula comes from <http://www.easyrgb.com/en/math.php>.
* https://github.com/cangoektas/xyz-to-lab/blob/master/src/index.js
* @param {number[]} color The CIE 1931 XYZ color to convert which refers to
* the D65/2° standard illuminant.
* @returns {number[]} The color in the CIE L*a*b* color space.
*/
// X, Y, Z of a "D65" light source.
// "D65" is a standard 6500K Daylight light source.
// https://en.wikipedia.org/wiki/Illuminant_D65
const D65 = [95.047, 100, 108.883]
export function xyzToLab([x, y, z]) {
[x, y, z] = [x, y, z].map((v, i) => {
v = v / D65[i]
return v > 0.008856 ? Math.pow(v, 1 / 3) : v * 7.787 + 16 / 116
})
const l = 116 * y - 16
const a = 500 * (x - y)
const b = 200 * (y - z)
return [l, a, b]
}
/**
* Converts Lab color space to Luminance-Chroma-Hue color space.
* http://www.brucelindbloom.com/index.html?Eqn_Lab_to_LCH.html
* @param {number[]}
* @return {number[]}
*/
export function labToLch([l, a, b]) {
const c = Math.sqrt(a * a + b * b)
const h = abToHue(a, b)
return [l, c, h]
}
/**
* Converts a and b of Lab color space to Hue of LCH color space.
* https://stackoverflow.com/questions/53733379/conversion-of-cielab-to-cielchab-not-yielding-correct-result
* @param {number} a
* @param {number} b
* @return {number}
*/
function abToHue(a, b) {
if (a >= 0 && b === 0) {
return 0
}
if (a < 0 && b === 0) {
return 180
}
if (a === 0 && b > 0) {
return 90
}
if (a === 0 && b < 0) {
return 270
}
let xBias
if (a > 0 && b > 0) {
xBias = 0
} else if (a < 0) {
xBias = 180
} else if (a > 0 && b < 0) {
xBias = 360
}
return radiansToDegrees(Math.atan(b / a)) + xBias
}
function radiansToDegrees(radians) {
return radians * (180 / Math.PI)
}
function degreesToRadians(degrees) {
return degrees * Math.PI / 180
}
/**
* Saturated colors appear brighter to human eye.
* That's called Helmholtz-Kohlrausch effect.
* Fairchild and Pirrotta came up with a formula to
* calculate a correction for that effect.
* "Color Quality of Semiconductor and Conventional Light Sources":
* https://books.google.ru/books?id=ptDJDQAAQBAJ&pg=PA45&lpg=PA45&dq=fairchild+pirrotta+correction&source=bl&ots=7gXR2MGJs7&sig=ACfU3U3uIHo0ZUdZB_Cz9F9NldKzBix0oQ&hl=ru&sa=X&ved=2ahUKEwi47LGivOvmAhUHEpoKHU_ICkIQ6AEwAXoECAkQAQ#v=onepage&q=fairchild%20pirrotta%20correction&f=false
* @return {number}
*/
function getLightnessUsingFairchildPirrottaCorrection([l, c, h]) {
const l_ = 2.5 - 0.025 * l
const g = 0.116 * Math.abs(Math.sin(degreesToRadians((h - 90) / 2))) + 0.085
return l + l_ * g * c
}
export function getPerceivedLightness(hex) {
return getLightnessUsingFairchildPirrottaCorrection(labToLch(xyzToLab(rgbToXyz(hex))))
}
方法可以根据您的需要而有所不同。以下是计算亮度的3种方法:
亮度(某些颜色空间的标准):(0.2126*R + 0.7152*G + 0.0722*B)光源 亮度(感知选项1):(0.299*R + 0.587*G + 0.114*B)光源 亮度(感知选项2,计算较慢):根号(0.241*R^2 + 0.691*G^2 + 0.068*B^2)→根号(0.299*R^2 + 0.587*G^2 + 0.114*B^2)(感谢@MatthewHerbst)来源
[编辑:添加了使用命名css颜色的例子,按每种方法排序。]