color.cpp
来自「这是VCF框架的代码」· C++ 代码 · 共 1,312 行 · 第 1/5 页
CPP
1,312 行
//Color.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*//* Generated by Together */#include "vcf/GraphicsKit/GraphicsKit.h"// NORMALIZED_HUE has the advantage that it is normalized between 0 and 1// but !NORMALIZED_HUE seems to me to have the advantage that a small change in luminosity doesn't find colors with different Hue when H is in [1-1/6, 1] ( like crimson )//#define NORMALIZED_HUEusing namespace VCF;#define VCF_RGB(r,g,b) ((ulong32)(((uchar)(r)|((ushort)((uchar)(g))<<8))|(((ulong32)(uchar)(b))<<16)))std::map<ColorNames::ColorID, String> ColorNames::nameMap;EnumeratorMapContainer<std::map<ColorNames::ColorID, String>, String > ColorNames::nameMapContainer;String ColorNames::unknownColorName_;const double ColorSpace::HueCriticalMax = HUECRITICALMAX; // Hue > HueCriticalMax => rgb.R > 1;///////////////////////////////////////////////////////////////////////////////// class Color - implementation//////////////////////////////////////////////////////////////////////// class ColorSpace - implementation//////////////////// Helper function to compute the luminosity for an RGB color.// Measures how bright the color is. I use this so I can draw the caption// text using the user's chosen color, unless it's too dark. See MSDN for// definition of luminosity and how to compute it.//int ColorSpace::getLuminosity( const Color& color ){ int r = (int)(color.r_ * Color::xFF + 0.5); int g = (int)(color.g_ * Color::xFF + 0.5); int b = (int)(color.b_ * Color::xFF + 0.5); int rgbMax = maxVal<>( maxVal<>(r,g), b); int rgbMin = minVal<>( minVal<>(r,g), b); return (int) (double) (((rgbMax+rgbMin) * ColorSpace::HSLMax) + ColorSpace::RGBMax ) / (2 * ColorSpace::RGBMax); /* // enhancing the precision ... debug later uint16 r, g, b; color.getRGB16(r, g, b); int rgbMax = maxVal<>( maxVal<>(r,g), b); int rgbMin = minVal<>( minVal<>(r,g), b); return (int) (double) (((double)(rgbMax+rgbMin) * ColorSpace::HSLMax16) + ColorSpace::RGBMax16 ) / (2 * ColorSpace::RGBMax16); */}void ColorSpace::setLuminosity( Color& color, const int& luminosity ){ HSLtype hslType = ColorToHSL( color ); double L = (double)luminosity / ColorSpace::HSLMax; hslType.L = L; color = HSLToColor( hslType );}ColorSpace::HSVtype ColorSpace::RGBToHSV( const RGBtype& rgb ){ // Code from: // Alvy Ray Smith and Eric Ray Lyons. HWB - A more intuitive hue-based color model. Journal of Graphics Tools, 1(1):3-17, 1996 // http://www.acm.org/jgt/papers/SmithLyons96/ // RGB are each on [0, 1]. S and V are returned on [0, 1] and H is // returned on [0, 6]. Exception: H is returned UNDEFINED if S==0. double R, G, B; SPLIT_RGB(rgb, R, G, B); double dMax, dMin, f; int i; HSVtype hsv; dMin = minVal<>( minVal<>(R, G), B); dMax = maxVal<>( maxVal<>(R, G), B); if (dMax == dMin) { MAKE_HSV ( hsv, ColorSpace::hue_undefined, 0, dMax ); return hsv; } f = (R == dMin) ? G - B : ((G == dMin) ? B - R : R - G); i = (R == dMin) ? 3 : ((G == dMin) ? 5 : 1); MAKE_HSV( hsv, i - f /(dMax - dMin), (dMax - dMin)/dMax, dMax ); return hsv;}ColorSpace::RGBtype ColorSpace::HSVToRGB( const HSVtype& hsv ){ // Code from: // Alvy Ray Smith and Eric Ray Lyons. HWB - A more intuitive hue-based color model. Journal of Graphics Tools, 1(1):3-17, 1996 // http://www.acm.org/jgt/papers/SmithLyons96/ // H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1]. // RGB are each returned on [0, 1]. double h, s, v; SPLIT_HSV(hsv, h, s, v); double m, n, f; int i; RGBtype rgb; if ( h == ColorSpace::hue_undefined ) { MAKE_RGB(rgb, v, v, v); return rgb; } i = static_cast<int>(floor(h)); f = h - i; if (!(i & 1)) { f = 1 - f; // if i is even } m = v * (1 - s); n = v * (1 - s * f); switch (i) { case 6: // no break here ! case 0: MAKE_RGB(rgb, v, n, m); break; case 1: MAKE_RGB(rgb, n, v, m); break; case 2: MAKE_RGB(rgb, m, v, n) break; case 3: MAKE_RGB(rgb, m, n, v); break; case 4: MAKE_RGB(rgb, n, m, v); break; case 5: MAKE_RGB(rgb, v, m, n); break; } return rgb;}double ColorSpace::HueToColorValue( double Hue, const double dMin, const double dMax ){ double V; double locHue = Hue; if (locHue < 0) { locHue = locHue + 1; } else { if (locHue > 1) { locHue = locHue - 1; } } if (6 * locHue < 1) { V = dMin + (dMax - dMin) * locHue * 6; } else { if (2 * locHue < 1) { V = dMax; } else { if (3 * locHue < 2) { V = dMin + (dMax - dMin) * (2/3 - locHue) * 6; } else { V = dMin; } } } return V; //return static_cast<byte>(floor (ColorSpace::RGBMax * V)); //round}ColorSpace::HSLtype ColorSpace::HSLRangeToHSL ( const HSLrangetype& hslRange ){ HSLtype hsl; //the casting is necessary hsl.H = (double) hslRange.H / (ColorSpace::HSLMax-1); hsl.S = (double) hslRange.S / ColorSpace::HSLMax; hsl.L = (double) hslRange.L / ColorSpace::HSLMax; return hsl;}ColorSpace::HSLrangetype ColorSpace::HSLToHSLRange ( const HSLtype& hsl ){ HSLrangetype hslRange; hslRange.H = static_cast<int>( floor ( hsl.H * (ColorSpace::HSLMax - 1) ) ); //round hslRange.S = static_cast<int>( floor ( hsl.S * ColorSpace::HSLMax ) ); //round hslRange.L = static_cast<int>( floor ( hsl.L * ColorSpace::HSLMax ) ); //round return hslRange;}double HueToColorValue ( double h, double dMin, double dMax ) { double v; if ( h < 0 ) { h += 1; } else if ( h > 1 ) { h -= 1; } if ( 6 * h < 1 ) { v = dMin + (dMax - dMin) * h * 6; } else if ( 2 * h < 1 ) { v = dMin; } else if ( 3 * h < 2 ) { v = dMin + (dMax - dMin) * (2/3 - h) * 6; } else { v = dMin; } return v;}ColorSpace::RGBtype ColorSpace::HSLToRGB ( const HSLtype& hsl ){ //from: http://www.undu.com/DN971201/00000019.htm and http://www.undu.com/DN971201/00000021.htm //from: http://www.vbaccelerator.com/codelib/gfx/clrman1.htm double H, S, L; SPLIT_HSL(hsl, H, S, L); double dMin, dMax; double R, G, B; if (S == 0) { //Achromatic case: //commented: H has been probably set manually. But this is not harmful anyway. //#ifdef _DEBUG // if ( H == ColorSpace::hue_undefined) { // StringUtils::traceWithArgs("HSLToRGB: Warning: function used with S==0 (h=%f, s=%f, l=%f\n", H, S, L ); // //assert( H == ColorSpace::hue_undefined ); // } //#endif R = L; //L; //static_cast<byte>(floor (ColorSpace::RGBMax * L)); //round //changed ! G = L; //R; //changed !
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?