improved codes of Color and ColorSpace

This commit is contained in:
2022-05-17 14:38:17 +08:00
parent f28508e98c
commit 14bca2e58f
4 changed files with 37 additions and 25 deletions

View File

@@ -9,16 +9,6 @@ namespace hgl
#define DEF_RGB_U8_TO_F(r,g,b) {float(r)/255.0f,float(g)/255.0f,float(b)/255.0f}
#define DEF_RGBA_U8_TO_F(r,g,b,a) {float(r)/255.0f,float(g)/255.0f,float(b)/255.0f,float(a)/255.0f}
inline constexpr uint8 RGB2Lum(const uint8 red,const uint8 green,const uint8 blue)
{
return float(red)*0.299+float(green)*0.587+float(blue)*0.114;
}
inline constexpr float RGB2Lumf(const float red,const float green,const float blue)
{
return red*0.299+green*0.587+blue*0.114;
}
const Color3f GetSpectralColor(const double l); ///<根据光谱值获取对应的RGB值
/**

View File

@@ -2,7 +2,6 @@
#define HGL_COLOR_SPACE_INCLUDE
#include<math.h>
#include<hgl/TypeFunc.h>
namespace hgl
{
@@ -20,33 +19,33 @@ namespace hgl
constexpr double SRGB_ALPHA =0.055f;
template<typename T>
inline constexpr T sRGB2Linear(const T &in)
inline constexpr T sRGB2Linear(const T &in,const T &gamma=GAMMA,const T &srgb_alpha=SRGB_ALPHA)
{
if(in<=0.4045)
return (double)in/12.92;
else
return pow((double(in)+SRGB_ALPHA)/(1.0f+SRGB_ALPHA),GAMMA);
return pow((double(in)+srgb_alpha)/(1.0f+srgb_alpha),gamma);
}
template<typename T>
inline constexpr T Linear2sRGB(const T &in)
inline constexpr T Linear2sRGB(const T &in,const T &inv_gamma=INV_GAMMA,const T &srgb_alpha=SRGB_ALPHA)
{
if(in<=0.0031308f)
return double(in)*12.92f;
else
return pow(double(in),INV_GAMMA)*(1.0f+SRGB_ALPHA)-SRGB_ALPHA;
return pow(double(in),inv_gamma)*(1.0f+srgb_alpha)-srgb_alpha;
}
template<typename T>
inline constexpr T sRGB2LinearCheaper(const T &in)
inline constexpr T sRGB2LinearCheaper(const T &in,const T &gamma=GAMMA)
{
return (T)pow(double(in),GAMMA);
return (T)pow(double(in),gamma);
}
template<typename T>
inline constexpr T Linear2sRGBCheaper(const T &in)
inline constexpr T Linear2sRGBCheaper(const T &in,const T &inv_gamma=INV_GAMMA)
{
return (T)pow((double)in,INV_GAMMA);
return (T)pow((double)in,inv_gamma);
}
template<typename T>
@@ -86,9 +85,30 @@ namespace hgl
return Clamp<T>(value,T(0),T(1));
}
template<> inline constexpr uint8 Clamp<uint8>(const uint8 &value)
template<typename T>
inline constexpr T RGB2Lum(const T &r,const T &g,const T &b)
{
return Clamp<uint8>(value,0,255);
return 0.299f*r+0.587f*g+0.114f*b;
}
template<typename T>
inline constexpr T RGB2Cb(const T &r,const T &g,const T &b)
{
return -0.168736f*r-0.331264f*g+0.5f*b;
}
template<typename T>
inline constexpr T RGB2Cr(const T &r,const T &g,const T &b)
{
return 0.5f*r-0.418688f*g-0.081312f*b;
}
template<typename T>
inline void RGB2YCbCr(T &y,T &cb,T &cr,const T &r,const T &g,const T &b)
{
y =RGB2Lum(r,g,b);
cb=RGB2Cb(r,g,b);
cr=RGB2Cr(r,g,b);
}
}//namespace hgl
#endif//HGL_COLOR_SPACE_INCLUDE

View File

@@ -1,5 +1,6 @@
#include<hgl/type/Color3f.h>
#include<hgl/type/Color.h>
#include<hgl/type/ColorSpace.h>
namespace hgl
{
void Color3f::Clamp()
@@ -16,8 +17,8 @@ namespace hgl
*/
void Color3f::To(float nr,float ng,float nb,float pos)
{
if(pos==0)return;
if(pos==1)
if(pos<=0)return;
if(pos>=1)
{
r=nr;
g=ng;

View File

@@ -1,5 +1,6 @@
#include<hgl/type/Color4f.h>
#include<hgl/type/Color.h>
#include<hgl/type/ColorSpace.h>
namespace hgl
{
void Color4f::Clamp()
@@ -17,8 +18,8 @@ namespace hgl
*/
void Color4f::To(float nr,float ng,float nb,float pos)
{
if(pos==0)return;
if(pos==1)
if(pos<=0)return;
if(pos>=1)
{
r=nr;
g=ng;