color.cpp

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

CPP
1,312
字号
		B = L;	//R;																//changed !	}	else	{		//Chromatic case:#ifdef SKIP_WRONG_VERSION			//delta = Max-Min			if (L <= 0.5) {				//s = (Max - Min) / (Max + Min)				// Get Min value:					dMax = L * (1 + S);					//dMin = L * (1 - S) ;				} else {					// s = (Max - Min) / (2 - Max - Min)				// Get Min value:					dMax = L + S - L * S;					//dMin = L - S * (1 - L) ;				}			dMin = 2 * L - dMax;			// Get the Max value:			//dMax = 2 * L - dMin ;			// Now depending on sector we can evaluate the h,l,s:			R = HueToColorValue (H + 1/3, dMin, dMax);			G = HueToColorValue (H, dMin, dMax);			B = HueToColorValue (H - 1/3, dMin, dMax);#else				//delta = Max-Min				if (L <= 0.5) {					//s = (Max - Min) / (Max + Min)					// Get Min value:					//dMax = L * (1 + S);						dMin = L * (1 - S) ;				} else {						// s = (Max - Min) / (2 - Max - Min)					// Get Min value:					//dMax = L + S - L * S;						dMin = L - S * (1 - L) ;				}				//dMin = 2 * L - dMax;				// Get the Max value:				dMax = 2 * L - dMin ;				// Now depending on sector we can evaluate the h,l,s:				//	R = HueToColourValue (H + 1/3, dMin, dMax);				//	G = HueToColourValue (H, dMin, dMax);				//	B = HueToColourValue (H - 1/3, dMin, dMax);					//here we have H in [0-1], not in [0-6], so...#ifdef NORMALIZED_HUE				if ( ColorSpace::HueCriticalMax < H ) {					H = H - 1;				}				H = H * 6;#endif				// Now depending on sector we can evaluate the H,L,S:				if (H < 1) {					R = dMax ;					if (H < 0) {						G = dMin ;						B = G - H * (dMax - dMin) ;					} else {						B = dMin ;						G = H * (dMax - dMin) + B ;					}				} else if (H < 3) {					G = dMax ;					if (H < 2) {						B = dMin ;						R = B - (H - 2) * (dMax - dMin) ;					} else {						R = dMin ;						B = (H - 2) * (dMax - dMin) + R ;					}				} else {					B = dMax ;					if (H < 4) {						R = dMin ;						G = R - (H - 4) * (dMax - dMin) ;					} else {						G = dMin ;						R = (H - 4) * (dMax - dMin) + G ;					}				}#endif  }	RGBtype rgb;	MAKE_RGB (rgb, R, G, B);  return rgb;}//		Coding the HSL Model////		Normally Hue is expressed as an angle between 0-360 to describe the colour//		and a value between 0 and 1 to describe Hue and Saturation.//		I have missed out the conversion to an angle in this implementation//		so the Hue works as follows://				Hue Value Colour//				-1 Magenta//				0 Red//				1 Yellow//				2 Green//				3 Aqua//				4 Blue//				5 MagentaColorSpace::HSLtype ColorSpace::RGBToHSL( const RGBtype& rgb ){	double R = rgb.R, G = rgb.G, B = rgb.B;	double D, dMax, dMin;	double H, S, L;	HSLtype hsl;	dMax = maxVal<> (R, maxVal<> (G, B));	dMin = minVal<> (R, minVal<> (G, B));	// calculate luminosity (this is the lightness)	L = (dMax + dMin) / 2;	// Next calculate saturation	if (dMax == dMin)	// it's gray	{		// Achromatic case		H = ColorSpace::hue_undefined; // it's actually undefined	//changed by M.P.		S = 0;		// commented: this is in my opinion the best solution: L unchanged, as adopted originally		//		in http://www.undu.com/DN971201/00000021.htm by RGBToHSL(), but L = dMin in RGBToHSV()		//L = dMin;	// commented	}	else	{		// Chromatic case		D = dMax - dMin;			// first calculate Saturation		if (L < 0.5) {			S = D / (dMax + dMin);		} else {			S = D / (2 - dMax - dMin);		}			// then calculate Hue		if (R == dMax) {			H = (G - B) / D;				// resulting color is between yellow and magenta		} else if (G == dMax) {			H  = 2 + (B - R) /D;		// resulting color is between cyan and yellow		} else {			H = 4 + (R - G) / D;		// resulting color is between magenta and cyan		}		//this next part is missed in http://www.undu.com/DN971201/00000021.htm because H is in [0-6]; but we want it in [0-1]#ifdef NORMALIZED_HUE		H = H / 6;		if (H < 0) {			H = H + 1;		}#endif	}	MAKE_HSL(hsl, H, S, L);	return hsl;}ColorSpace::RGBrangetype ColorSpace::RGBToRGBRange( const RGBtype& rgb ){	RGBrangetype rgbRange;	MAKE_RGB( rgbRange, static_cast<int>(rgb.R * ColorSpace::RGBMax + 0.5),		// + 0.5 to compensate rounding errors + static_cast<int>( ((double)(3.0/255))*255 = 2) !						static_cast<int>(rgb.G * ColorSpace::RGBMax + 0.5),						static_cast<int>(rgb.B * ColorSpace::RGBMax + 0.5) );	return rgbRange;}ColorSpace::RGBtype ColorSpace::RGBRangeToRGB(const RGBrangetype& rgbRange){	RGBtype rgb;	MAKE_RGB ( rgb, (double)rgbRange.R / ColorSpace::RGBMax,					(double)rgbRange.G / ColorSpace::RGBMax,					(double)rgbRange.B / ColorSpace::RGBMax );	// casting is necessary	return rgb;}ulong32 ColorSpace::RGBToColorLong (const RGBtype& rgb){	return VCF_RGB( rgb.R * ColorSpace::RGBMax, rgb.G * ColorSpace::RGBMax, rgb.B * ColorSpace::RGBMax );}ColorSpace::RGBtype ColorSpace::ColorLongToRGB ( const ulong32 color ){	RGBtype rgb;	Color c(color);	//the casting is necessary	rgb.R = (double) (c.r_ * Color::xFF) / ColorSpace::RGBMax;	rgb.G = (double) (c.g_ * Color::xFF) / ColorSpace::RGBMax;	rgb.B = (double) (c.b_ * Color::xFF) / ColorSpace::RGBMax;	return rgb;}ulong32 ColorSpace::HSLToColorLong (const HSLtype& hsl){	RGBtype rgb;	rgb = HSLToRGB (hsl);	return VCF_RGB(rgb.R * ColorSpace::RGBMax, rgb.G * ColorSpace::RGBMax, rgb.B * ColorSpace::RGBMax);}ColorSpace::HSLtype ColorSpace::ColorLongToHSL ( const ulong32 color ){	RGBtype rgb;	//the casting is necessary	Color c(color);	rgb.R = (double) (c.r_ * Color::xFF) / ColorSpace::RGBMax;	rgb.G = (double) (c.g_ * Color::xFF) / ColorSpace::RGBMax;	rgb.B = (double) (c.b_ * Color::xFF) / ColorSpace::RGBMax;	return RGBToHSL(rgb);}ColorSpace::RGBtype ColorSpace::HSLRangeToRGB ( const HSLrangetype& hslRange ){	HSLtype hsl = HSLRangeToHSL(hslRange);	return HSLToRGB (hsl);}ColorSpace::HSLrangetype ColorSpace::RGBToHSLRange ( const RGBtype& rgb ){	HSLtype hsl;	hsl = RGBToHSL (rgb);	HSLrangetype hslRange = HSLToHSLRange(hsl);	return hslRange;}ulong32 ColorSpace::HSLRangeToColorLong ( const HSLrangetype& hslRange ){	RGBtype rgb = HSLRangeToRGB (hslRange);	return VCF_RGB(rgb.R, rgb.G, rgb.B);}ColorSpace::HSLrangetype ColorSpace::ColorLongToHSLRange ( ulong32 color ){	RGBtype rgb;	Color c(color);	//the casting is necessary	rgb.R = (double) (c.r_ * Color::xFF) / ColorSpace::RGBMax;	rgb.G = (double) (c.g_ * Color::xFF)/ ColorSpace::RGBMax;	rgb.B = (double) (c.b_ * Color::xFF) / ColorSpace::RGBMax;	return RGBToHSLRange (rgb);}ColorSpace::HWBtype ColorSpace::RGBToHWB( 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]. W and B are returned on [0, 1] and H is	// returned on [0, 6]. Exception: H is returned UNDEFINED if W == 1 - B.

⌨️ 快捷键说明

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