PHP - How to convert RGB color to CIE 1931 color specification
Asked Answered
G

2

3

I am creating my own PHP based application where i want to change RGB color into xy format of CIE 1931.

How can i convert my RGB color specs to the CIE color space?

Gem answered 29/11, 2013 at 10:19 Comment(0)
E
10

First calculate X, Y and Z with the transform matrix and then normalize the result

X = 0.4124*R + 0.3576*G + 0.1805*B
Y = 0.2126*R + 0.7152*G + 0.0722*B
Z = 0.0193*R + 0.1192*G + 0.9505*B

Normalize:

x = X / (X + Y + Z)
y = Y / (X + Y + Z)
Extortionary answered 29/11, 2013 at 10:25 Comment(2)
does this assume RGB is a float or an integer. If it's an integer do you normalize by dividing by 255?Prostyle
I think it should be between 0 and 1.0 - normalize it by divide by 256Extortionary
R
2

Here is my Javascript version. It should help you enough ;)

 /**
 * Convert RGB to XY
 */
function RGBtoXY(red,green,blue){
    red = (red > 0.04045) ? Math.pow((red + 0.055) / (1.0 + 0.055), 2.4) : (red / 12.92);
    green = (green > 0.04045) ? Math.pow((green + 0.055) / (1.0 + 0.055), 2.4) : (green / 12.92);
    blue = (blue > 0.04045) ? Math.pow((blue + 0.055) / (1.0 + 0.055), 2.4) : (blue / 12.92);
    var X = red * 0.664511 + green * 0.154324 + blue * 0.162028;
    var Y = red * 0.283881 + green * 0.668433 + blue * 0.047685;
    var Z = red * 0.000088 + green * 0.072310 + blue * 0.986039;
    var fx = X / (X + Y + Z);
    var fy = Y / (X + Y + Z);
    return [fx.toPrecision(4),fy.toPrecision(4)];
}
Repentant answered 17/3, 2016 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.