@@ -378,4 +378,50 @@ vec2 S_RotateVec2(vec2 v, float angle)
378378 float s = sin (angle);
379379 mat2 m = mat2 (c, - s, s, c);
380380 return m * v;
381+ }
382+
383+ // https://web.archive.org/web/20191027010220/http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
384+ vec3 S_ConvertRgbToXyz(vec3 rgb)
385+ {
386+ vec3 xyz;
387+ xyz.x = dot (vec3 (0.4124564 , 0.3575761 , 0.1804375 ), rgb);
388+ xyz.y = dot (vec3 (0.2126729 , 0.7151522 , 0.0721750 ), rgb);
389+ xyz.z = dot (vec3 (0.0193339 , 0.1191920 , 0.9503041 ), rgb);
390+ return xyz;
391+ }
392+
393+ vec3 S_ConvertXyzToRgb(vec3 xyz)
394+ {
395+ vec3 rgb;
396+ rgb.x = dot (vec3 (3.2404542 , - 1.5371385 , - 0.4985314 ), xyz);
397+ rgb.y = dot (vec3 (- 0.9692660 , 1.8760108 , 0.0415560 ), xyz);
398+ rgb.z = dot (vec3 (0.0556434 , - 0.2040259 , 1.0572252 ), xyz);
399+ return rgb;
400+ }
401+
402+ // https://web.archive.org/web/20191027010144/http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_xyY.html
403+ vec3 S_ConvertXyzToYxy(vec3 xyz)
404+ {
405+ float inv = 1.0 / dot (xyz, vec3 (1.0 , 1.0 , 1.0 ));
406+ return vec3 (xyz.y, xyz.x * inv, xyz.y * inv);
407+ }
408+
409+ // https://web.archive.org/web/20191027010036/http://www.brucelindbloom.com/index.html?Eqn_xyY_to_XYZ.html
410+ vec3 S_ConvertYxyToXyz(vec3 Yxy)
411+ {
412+ vec3 xyz;
413+ xyz.x = Yxy.x * Yxy.y / Yxy.z;
414+ xyz.y = Yxy.x;
415+ xyz.z = Yxy.x * (1.0 - Yxy.y - Yxy.z) / Yxy.z;
416+ return xyz;
417+ }
418+
419+ vec3 convertRGB2Yxy(vec3 rgb)
420+ {
421+ return S_ConvertXyzToYxy(S_ConvertRgbToXyz(rgb));
422+ }
423+
424+ vec3 convertYxy2RGB(vec3 Yxy)
425+ {
426+ return S_ConvertXyzToRgb(S_ConvertYxyToXyz(Yxy));
381427}
0 commit comments