Sunday, January 11, 2015

YIQ to sRGB color space conversion

C++11 code to convert YIQ to sRGB.
auto clamp = [](double v, double min, double max) { return v < min ? min : v > max ? max : v; }; //Y'IQ -> NTSC R'G'B' //http://en.wikipedia.org/wiki/YIQ double ntsc_r = y + 0.956 * i + 0.620 * q; double ntsc_g = y + -0.272 * i + -0.647 * q; double ntsc_b = y + -1.108 * i + 1.705 * q; //NTSC R'G'B' -> linear NTSC RGB ntsc_r = pow(clamp(ntsc_r, 0.0, 1.0), 2.2); ntsc_g = pow(clamp(ntsc_g, 0.0, 1.0), 2.2); ntsc_b = pow(clamp(ntsc_b, 0.0, 1.0), 2.2); //NTSC RGB -> sRGB //Adapated from conversion matrices at http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html //[CIE xyz -> sRGB] * [SMTPE-C RGB -> CIE xyz] double srgb_r = ntsc_r * 0.939555319482800 + ntsc_g * 0.0501723952684700 + ntsc_b * 0.0102725646454400; double srgb_g = ntsc_r * 0.0177757796807600 + ntsc_g * 0.965792853854560 + ntsc_b * 0.0164314174435600; double srgb_b = ntsc_r * -0.00162232670898000 + ntsc_g * -0.00437074564609001 + ntsc_b * 1.00599294870830; //sRGB -> sR'G'B' srgb_r = pow(clamp(srgb_r, 0.0, 1.0), 1.0/2.2); srgb_g = pow(clamp(srgb_g, 0.0, 1.0), 1.0/2.2); srgb_b = pow(clamp(srgb_b, 0.0, 1.0), 1.0/2.2);

No comments:

Post a Comment