Color Color::toRGB(bool internal_check) const { float R, G, B; float X, Y, Z; switch (space) { case RGB: return *this; case CMY: CMY_to_RGB(a, b, c, R, G, B); break; case HLS: HLS_to_RGB(a, b, c, R, G, B); break; case HSV: HSV_to_RGB(a, b, c, R, G, B); break; case CIEXYZ: //convert to RGB709 XYZ_to_RGB709(a, b, c, R, G, B); break; case CIExyY: xyY_to_XYZ(a, b, c, X, Y, Z); if (internal_check) XYZ_check(X, Y, Z); XYZ_to_RGB709(X, Y, Z, R, G, B); break; case CIELuv: Luv_to_XYZ(a, b, c, X, Y, Z); if (internal_check) XYZ_check(X, Y, Z); XYZ_to_RGB709(X, Y, Z, R, G, B); break; default: return Color(0.0, 0.0, 0.0, INVALID); } RGB_check(R, G, B); return Color(R, G, B, RGB); }
/** * Set the whitepoint reference. * * In additive image reproduction, the white point is the chromaticity of * the colour reproduced by equal red, green and blue components. White * point is a function of the ratio (or balance) of power among the * primaries. In subtractive reproduction, white is the SPD of the * illumination, multiplied by the SPD of the media. There is no unique * physical or perceptual definition of white, so to achieve accurate * colour interchange you must specify the characteristics of your white. * * It is often convenient for purposes of calculation to define white as * a uniform SPD. This white reference is known as the equal-energy * illuminant, or CIE Illuminant E. * * (text excerpt from Poynton's Color FAQ, Copyright Charles Poynton 1997) */ void setWhite(float x, float y, float /*z*/) { float X, Y, Z; xyY_to_XYZ(x, y, 1.0, X, Y, Z); XYZ_check(X, Y, Z); Xn = X; Yn = Y; Zn = Z; XYZ_to_uvwp(X, Y, Z, unprime, vnprime, wnprime); }
Color Color::toCIELuv(bool internal_check) const { float L, u, v; float X, Y, Z; float R, G, B; switch (space) { case CIELuv: return *this; case CIExyY: xyY_to_XYZ(a, b, c, X, Y, Z); if (internal_check) XYZ_check(X, Y, Z); XYZ_to_Luv(X, Y, Z, L, u, v); break; case CIEXYZ: XYZ_to_Luv(a, b, c, L, u, v); break; case RGB: RGB709_to_XYZ(a, b, c, X, Y, Z); if (internal_check) XYZ_check(X, Y, Z); XYZ_to_Luv(X, Y, Z, L, u, v); case CMY: CMY_to_RGB(a, b, c, R, G, B); if (internal_check) RGB_check(R, G, B); RGB709_to_XYZ(R, G, B, X, Y, Z); if (internal_check) XYZ_check(X, Y, Z); XYZ_to_Luv(X, Y, Z, L, u, v); break; case HLS: HLS_to_RGB(a, b, c, R, G, B); if (internal_check) RGB_check(R, G, B); RGB709_to_XYZ(R, G, B, X, Y, Z); if (internal_check) XYZ_check(X, Y, Z); XYZ_to_Luv(X, Y, Z, L, u, v); break; case HSV: HSV_to_RGB(a, b, c, R, G, B); if (internal_check) RGB_check(R, G, B); RGB709_to_XYZ(R, G, B, X, Y, Z); if (internal_check) XYZ_check(X, Y, Z); XYZ_to_Luv(X, Y, Z, L, u, v); break; default: return Color(0.0, 0.0, 0.0, INVALID); } Luv_check(L, u, v); return Color(L, u, v, CIELuv); }
Color3 ShadingSystemImpl::to_rgb (ustring fromspace, float a, float b, float c) { if (fromspace == Strings::RGB || fromspace == Strings::rgb) return Color3 (a, b, c); if (fromspace == Strings::hsv) return hsv_to_rgb (a, b, c); if (fromspace == Strings::hsl) return hsl_to_rgb (a, b, c); if (fromspace == Strings::YIQ) return YIQ_to_rgb (a, b, c); if (fromspace == Strings::XYZ) return XYZ_to_RGB (a, b, c); if (fromspace == Strings::xyY) return XYZ_to_RGB (xyY_to_XYZ (Color3(a,b,c))); error ("Unknown color space \"%s\"", fromspace.c_str()); return Color3 (a, b, c); }
Color Color::toHLS(bool internal_check) const { float H, L, S; float R, G, B; float X, Y, Z; switch (space) { case HLS: return *this; case CIELuv: H = atan2f(c, b); L = a; S = sqrtf(b+c); break; case CIExyY: xyY_to_XYZ(a, b, c, X, Y, Z); if (internal_check) XYZ_check(X, Y, Z); XYZ_to_RGB709(X, Y, Z, R, G, B); if (internal_check) RGB_check(R, G, B); RGB_to_HLS(R, G, B, H, L, S); break; case CIEXYZ: XYZ_to_RGB709(a, b, c, R, G, B); if (internal_check) RGB_check(R, G, B); RGB_to_HLS(R, G, B, H, L, S); break; case RGB: RGB_to_HLS(a, b, c, H, L, S); break; case CMY: CMY_to_RGB(a, b, c, R, G, B); if (internal_check) RGB_check(R, G, B); RGB_to_HLS(R, G, B, H, L, S); break; case HSV: HSV_to_RGB(a, b, c, R, G, B); if (internal_check) RGB_check(R, G, B); RGB_to_HLS(R, G, B, H, L, S); break; default: return Color(0.0, 0.0, 0.0, INVALID); } HLS_check(H, L, S); return Color(H, L, S, HLS); }