From: Mikko Rasa Date: Mon, 21 Apr 2014 07:36:17 +0000 (+0300) Subject: Move sRGB formulas to color.h X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f07dffb230b53efcf5e2706493b065f020077fdc;p=libs%2Fgl.git Move sRGB formulas to color.h --- diff --git a/source/color.h b/source/color.h index c45751d0..e57689bf 100644 --- a/source/color.h +++ b/source/color.h @@ -1,9 +1,17 @@ #ifndef MSP_GL_COLOR_H_ #define MSP_GL_COLOR_H_ +#include + namespace Msp { namespace GL { +inline float to_srgb(float c) +{ return (c<=0.0031308f ? 12.92f*c : 1.055f*std::pow(c, 1/2.4f)-0.055f); } + +inline float to_linear(float c) +{ return (c<=0.04045 ? c/12.92f : std::pow((c+0.055f)/1.055f, 2.4f)); } + struct Color { float r, g, b, a; @@ -16,6 +24,9 @@ struct Color Color operator+(const Color &c) const { return Color(r+c.r, g+c.g, b+c.b, 1-(1-a)*(1-c.a)); } bool operator==(const Color &c) const { return (r==c.r && g==c.g && b==c.b && a==c.a); } bool operator!=(const Color &c) const { return !operator==(c); } + + Color to_srgb() const { return Color(GL::to_srgb(r), GL::to_srgb(g), GL::to_srgb(b), a); } + Color to_linear() const { return Color(GL::to_linear(r), GL::to_linear(g), GL::to_linear(b), a); } }; } // namespace GL diff --git a/source/colorcurve.cpp b/source/colorcurve.cpp index 6351ea20..6b456dfe 100644 --- a/source/colorcurve.cpp +++ b/source/colorcurve.cpp @@ -1,4 +1,5 @@ #include +#include "color.h" #include "colorcurve.h" #include "mesh.h" #include "shader.h" @@ -88,7 +89,7 @@ void ColorCurve::set_srgb() unsigned char curve_data[256]; curve_data[0] = 0; for(unsigned i=1; i<256; ++i) - curve_data[i] = (1.055*pow(i/255.0f, 1/2.4f)-0.055)*255+0.5; + curve_data[i] = to_srgb(i/255.0f)*255+0.5f; curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data); }