color.cpp

来自「这是VCF框架的代码」· C++ 代码 · 共 1,312 行 · 第 1/5 页

CPP
1,312
字号
	double R, G, B;	SPLIT_RGB (rgb, R, G, B);	double w, v, b, f;	int i;	HWBtype hwb;	w = minVal<>( minVal<>(R, G), B);	v = maxVal<>( maxVal<>(R, G), B);	b = 1 - v;	if (v == w)	{		// Achromatic case		MAKE_HWB( hwb, ColorSpace::hue_undefined, w, b );	}	else	{		// Chromatic case		f = (R == w) ? G - B : ((G == w) ? B - R : R - G);		i = (R == w) ? 3 : ((G == w) ? 5 : 1);		MAKE_HWB( hwb, i - f /(v - w), w, b );	}	return hwb;}ColorSpace::RGBtype ColorSpace::HWBToRGB( const HWBtype& hwb ){	// 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. W and B are given on [0, 1].	// RGB are each returned on [0, 1].	double h, w, b;	SPLIT_HWB(hwb, h, w, b);	double v, n, f;	int i;	RGBtype rgb;	v = 1 - b;	if ( h == ColorSpace::hue_undefined )	{		// Achromatic case		MAKE_RGB(rgb, v, v, v);	}	else	{		// Chromatic case		i = static_cast<int>(floor(h));		f = h - i;		if (i & 1) {		 f = 1 - f; // if i is odd		}		n = w + f * (v - w); // linear interpolation between w and v		switch (i) {		case 6: 	// no break here !		case 0:			MAKE_RGB(rgb, v, n, w);			break;		case 1:			MAKE_RGB(rgb, n, v, w);			break;		case 2:			MAKE_RGB(rgb, w, v, n);			break;		case 3:			MAKE_RGB(rgb, w, n, v);			break;		case 4:			MAKE_RGB(rgb, n, w, v);			break;		case 5:			MAKE_RGB(rgb, v, w, n);			break;		}	}	return rgb;}////////////////////////////////////////////////////////////////////////void ColorSpace::changeHue( HSLtype& hsl, const double& deltaH ) {	double H2 = hsl.H + deltaH;#ifdef NORMALIZED_HUE	if ( H2 > ColorSpace::HueCriticalMax) {		H2 -= 1;	}#else	if ( H2 > 5 ) {		H2 -= 1;	}#endif	//	if ( H2 < 0) {	//		H2 = 0;	//	}	hsl.H = H2;}double ColorSpace::getChanged( const double& initialVal, const double& percent ){	//if p===percentage: we add p to what is between the actual value	//										and we subtract what is between the actual value and 0	double val = initialVal;	double pct = percent;	if ( pct > 1 ) {		pct = 1;	}	if ( pct != 0 ) {		if ( pct > 0 ) {			//we add p to what is between the actual value			//	more: x + (1-x)*p === x*(1-p) + p			val = val * ( 1-pct ) + pct;		} else {			// we subtract what is between the actual value and 0			//	less: x - x*p  === x*(1-p)			val *= ( 1+pct );		}	}	return val;}void ColorSpace::changeHSV ( HSVtype& hsv, const double& percentH, const double& percentS, const double& percentV ){	// suggested with colors: 0.0/ 0.0 / 0.71428571428571	// suggested with grays:  0.0/ 0.0 / 0.33333333333333	hsv.H = getChanged( hsv.H, percentH );	// better color	hsv.S = getChanged( hsv.S, percentS );	// better color	hsv.V = getChanged( hsv.V, percentV );}void ColorSpace::changeHSL ( HSLtype& hsl, const double& percentH, const double& percentS, const double& percentL ){	// suggested with colors: 0.0/ 0.0 / -0.71428571428571	// suggested with grays:  0.0/ 0.0 / -0.33333333333333	hsl.H = getChanged( hsl.H, percentH );	// better color	hsl.S = getChanged( hsl.S, percentS );	// better color	hsl.L = getChanged( hsl.L, percentL );	// this counts too}void ColorSpace::changeHWB ( HWBtype& hwb, const double& percentH, const double& percentW, const double& percentB ){	// suggested with colors: 0.0/ 0.0 / 0.71428571428571	// suggested with grays:  0.0/ 0.0 / 0.33333333333333	hwb.H = getChanged( hwb.H, percentH );	//better color	hwb.W = getChanged( hwb.W, percentW );	hwb.B = getChanged( hwb.B, percentB );}Color Color::getColorContrast( const Color& color, double deltaL/*=0.3*/ ){	double deltaLum = deltaL;	Color clrCnt = color;	Color clrTst = color;//#define TEST_RGBToHSLToRGB#ifdef TEST_RGBToHSLToRGB	//ColorSpace::HSLtype _hslTst = ColorSpace::ColorToHSL( clrTst );	ColorSpace::RGBtype rgbTst = ColorSpace::ColorToRGB( clrTst );	ColorSpace::HSLtype _hslTst = ColorSpace::RGBToHSL ( rgbTst );	//Color clrTest = ColorSpace::HSLToColor( _hslTst );	ColorSpace::RGBtype rgbTest = ColorSpace::HSLToRGB (_hslTst);	Color clrTest = ColorSpace::RGBToColor( rgbTest );#endif	ColorSpace::HSLtype _hslCnt = ColorSpace::ColorToHSL( clrCnt );	int lumin = clrCnt.getLuminosity();	// not enough contrast ?	if ( lumin < 128 ) {		if ( 0 < deltaLum ) {			_hslCnt.L = ColorSpace::getChanged( _hslCnt.L, +deltaLum); // lighter		} else {			_hslCnt.L = ColorSpace::getChanged( _hslCnt.L, -deltaLum); // lighter		}	} else {		if ( 0 < deltaLum ) {			_hslCnt.L = ColorSpace::getChanged( _hslCnt.L, -deltaLum); // darker		} else {			_hslCnt.L = ColorSpace::getChanged( _hslCnt.L, +deltaLum); // darker		}	}	clrCnt = ColorSpace::HSLToColor( _hslCnt );	return clrCnt;}///////////////////////////////////////////////////////////////////////////////// Color implementationColor::Color( const String& colorName ) {	( *this ) = (* GraphicsToolkit::getColorFromColormap( colorName ) );}String Color::toHexCode8( const ColorPackScheme& cps, const ColorType& ct ){	// Remark:	//   scheme: 0x00RRGGBB ( it would appear as BB GG RR 00 with Intel architecture ) so it should be:	//   code = StringUtils::format( L"%02x%02x%02x", cb, cg, cr ); if we want to see as it would appear with Intel architecture	String code = "";	uint8 ua, ub, uc;	double a, b, c;	switch ( ct ) {		case Color::ctRGB:			a = r_;			b = g_;			c = b_;			break;		case Color::ctHSV: {			ColorSpace::RGBtype rgb;			MAKE_RGB( rgb, r_, g_ , b_ );			ColorSpace::HSVtype hsv = ColorSpace::RGBToHSV(rgb);			SPLIT_HSV( hsv, a, b, c );			}			break;		case Color::ctHSL: {			ColorSpace::RGBtype rgb;			MAKE_RGB( rgb, r_, g_ , b_ );			ColorSpace::HSLtype hsl = ColorSpace::RGBToHSL(rgb);			SPLIT_HSL( hsl, a, b, c );			}			break;		case Color::ctHWB: {			ColorSpace::RGBtype rgb;			MAKE_RGB( rgb, r_, g_ , b_ );			ColorSpace::HWBtype hwb = ColorSpace::RGBToHWB(rgb);			SPLIT_HWB( hwb, a, b, c );			}			break;	}	ua = (uint8)(a * Color::xFF + 0.5);	ub = (uint8)(b * Color::xFF + 0.5);	uc = (uint8)(c * Color::xFF + 0.5);	switch ( cps ) {		case Color::cpsARGB : {			//uint32 rgb = 0;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?