2011-10-10 19:35:21 +00:00
|
|
|
// This code is in the public domain -- castanyo@yahoo.es
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#ifndef NV_MATH_COLOR_INL
|
|
|
|
#define NV_MATH_COLOR_INL
|
|
|
|
|
|
|
|
#include "Color.h"
|
|
|
|
#include "Vector.inl"
|
|
|
|
|
|
|
|
|
|
|
|
namespace nv
|
|
|
|
{
|
|
|
|
|
2011-10-11 06:40:40 +00:00
|
|
|
// Clamp color components.
|
2011-10-10 19:35:21 +00:00
|
|
|
inline Vector3 colorClamp(Vector3::Arg c)
|
|
|
|
{
|
2012-07-20 16:19:03 +00:00
|
|
|
return Vector3(saturate(c.x), saturate(c.y), saturate(c.z));
|
2011-10-10 19:35:21 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 06:40:40 +00:00
|
|
|
// Clamp without allowing the hue to change.
|
2011-10-10 19:35:21 +00:00
|
|
|
inline Vector3 colorNormalize(Vector3::Arg c)
|
|
|
|
{
|
|
|
|
float scale = 1.0f;
|
|
|
|
if (c.x > scale) scale = c.x;
|
|
|
|
if (c.y > scale) scale = c.y;
|
|
|
|
if (c.z > scale) scale = c.z;
|
|
|
|
return c / scale;
|
|
|
|
}
|
|
|
|
|
2011-10-11 06:40:40 +00:00
|
|
|
// Convert Color32 to Color16.
|
2011-10-10 19:35:21 +00:00
|
|
|
inline Color16 toColor16(Color32 c)
|
|
|
|
{
|
|
|
|
Color16 color;
|
|
|
|
// rrrrrggggggbbbbb
|
|
|
|
// rrrrr000gggggg00bbbbb000
|
2011-10-11 06:40:40 +00:00
|
|
|
// color.u = (c.u >> 3) & 0x1F;
|
|
|
|
// color.u |= (c.u >> 5) & 0x7E0;
|
|
|
|
// color.u |= (c.u >> 8) & 0xF800;
|
2011-10-10 19:35:21 +00:00
|
|
|
|
|
|
|
color.r = c.r >> 3;
|
|
|
|
color.g = c.g >> 2;
|
|
|
|
color.b = c.b >> 3;
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-11 06:40:40 +00:00
|
|
|
// Promote 16 bit color to 32 bit using regular bit expansion.
|
2011-10-10 19:35:21 +00:00
|
|
|
inline Color32 toColor32(Color16 c)
|
|
|
|
{
|
|
|
|
Color32 color;
|
2011-10-11 06:40:40 +00:00
|
|
|
// c.u = ((col0.u << 3) & 0xf8) | ((col0.u << 5) & 0xfc00) | ((col0.u << 8) & 0xf80000);
|
|
|
|
// c.u |= (c.u >> 5) & 0x070007;
|
|
|
|
// c.u |= (c.u >> 6) & 0x000300;
|
2011-10-10 19:35:21 +00:00
|
|
|
|
|
|
|
color.b = (c.b << 3) | (c.b >> 2);
|
|
|
|
color.g = (c.g << 2) | (c.g >> 4);
|
|
|
|
color.r = (c.r << 3) | (c.r >> 2);
|
|
|
|
color.a = 0xFF;
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Color32 toColor32(Vector4::Arg v)
|
|
|
|
{
|
|
|
|
Color32 color;
|
2012-07-20 16:19:03 +00:00
|
|
|
color.r = uint8(saturate(v.x) * 255);
|
|
|
|
color.g = uint8(saturate(v.y) * 255);
|
|
|
|
color.b = uint8(saturate(v.z) * 255);
|
|
|
|
color.a = uint8(saturate(v.w) * 255);
|
2011-10-10 19:35:21 +00:00
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Vector4 toVector4(Color32 c)
|
|
|
|
{
|
|
|
|
const float scale = 1.0f / 255.0f;
|
|
|
|
return Vector4(c.r * scale, c.g * scale, c.b * scale, c.a * scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline float perceptualColorDistance(Vector3::Arg c0, Vector3::Arg c1)
|
|
|
|
{
|
|
|
|
float rmean = (c0.x + c1.x) * 0.5f;
|
|
|
|
float r = c1.x - c0.x;
|
|
|
|
float g = c1.y - c0.y;
|
|
|
|
float b = c1.z - c0.z;
|
|
|
|
return sqrtf((2 + rmean)*r*r + 4*g*g + (3 - rmean)*b*b);
|
|
|
|
}
|
|
|
|
|
2012-07-20 16:19:03 +00:00
|
|
|
|
|
|
|
inline float hue(float r, float g, float b) {
|
|
|
|
float h = atan2f(sqrtf(3.0f)*(g-b), 2*r-g-b) * (1.0f / (2 * PI)) + 0.5f;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-10 19:35:21 +00:00
|
|
|
} // nv namespace
|
|
|
|
|
|
|
|
#endif // NV_MATH_COLOR_INL
|