Exemplo n.º 1
0
	ColorHSLx<float, Colorf>::ColorHSLx(const Colorf &color)
	{
		float red = color.get_red();
		float green = color.get_green();
		float blue = color.get_blue();

		const float min_value = min(red, min(blue, green));
		const float max_value = max(red, max(blue, green));
		a = color.get_alpha();
		l = (max_value + min_value) / 2.0f;

		if (min_value == max_value)
		{
			h = 0.0f;
			s = 0.0f;
			return;
		}

		if (l <= 0.5f)
		{
			s = (max_value - min_value) / (min_value + max_value);
		}
		else
		{
			s = (max_value - min_value) / (2.0f - (min_value + max_value));
		}

		if (max_value == red)
		{
			h = (60.0f * (green - blue)) / (max_value - min_value);
		}
		else if (max_value == green)
		{
			h = 120.0f + (60.0f * (blue - red)) / (max_value - min_value);
		}
		else
		{
			h = 240.0f + (60.0f * (red - green)) / (max_value - min_value);
		}
	}
Exemplo n.º 2
0
ColorHSVx<float, Colorf>::ColorHSVx(const Colorf &color)
{
	float red = color.get_red();
	float green = color.get_green();
	float blue = color.get_blue();

	const float min_value = min(red,min(blue, green));
	const float max_value = max(red,max(blue, green));
	a = color.get_alpha();

	if (min_value == max_value)
	{
		h = 0.0f;
		s = 0.0f;
		v = min_value;
		return;
	}

	s = max_value != 0.0f ? (max_value - min_value) / max_value : 0.0f;
	v = max_value;
	
	float r = float(max_value - red) / (max_value - min_value);
	float g = float(max_value - green) / (max_value - min_value);
	float b = float(max_value - blue) / (max_value - min_value);

	if (max_value == red)
	{
		h = (60.0f * (b - g));
	}
	else if (max_value == green)
	{
		h = (60.0f * (2.0f + r - b));
	}
	else
	{
		h = (60.0f * (4.0f + g - r));
	}
}