• Main Page
  • Classes
  • Files
  • File List

T:/Eigene Dateien/Visual Studio 2008/Projects/VisLuFramework/src/Colours.cpp

00001 #include "Colours.h"
00002 
00003 #include "math.h"
00004 
00005 using namespace Colours;
00006 
00007 namespace Colours
00008 {
00009 
00010         float toX(GLubyte R, GLubyte G, GLubyte B)
00011         {
00012                 return toX(R/255.f, G/255.f, B/255.f);
00013         }
00014 
00015         float toY(GLubyte R, GLubyte G, GLubyte B)
00016         {
00017                 return toY(R/255.f, G/255.f, B/255.f);
00018         }
00019 
00020         float toZ(GLubyte R, GLubyte G, GLubyte B)
00021         {
00022                 return toZ(R/255.f, G/255.f, B/255.f);
00023         }
00024 
00025         float toX(float R, float G, float B)
00026         {
00027                 return (0.4124564f * R + 0.3575761f * G + 0.1804375f * B);
00028         }
00029 
00030         float toY(float R, float G, float B)
00031         {
00032                 return (0.2126729f * R + 0.7151522f * G + 0.0721750f * B);
00033         }
00034 
00035         float toZ(float R, float G, float B)
00036         {
00037                 return (0.0193339f * R + 0.1191920f * G + 0.9503041f * B);
00038         }
00039 
00040         float toL(float Y)
00041         {
00042                 return (116.f * YNormCalc(Y) - 16.f);
00043         }
00044         float toA(float X, float Y)
00045         {
00046                 return (500.f * (XNormCalc(X) - YNormCalc(Y)));
00047         }
00048         float toB(float Y, float Z)
00049         {
00050                 return (200.f * (YNormCalc(Y) - ZNormCalc(Z)));
00051         }
00052 
00053         float XNormCalc(float X)
00054         {
00055                 float div = (X / Xn);
00056                 return normCalc(div);
00057 
00058         }
00059         float YNormCalc(float Y)
00060         {
00061                 float div = (Y / Yn);
00062                 return normCalc(div);
00063         }
00064 
00065         float ZNormCalc(float Z)
00066         {
00067                 float div = (Z / Zn);
00068                 return normCalc(div);
00069         }
00070 
00071         float normCalc( float div )
00072         {
00073                 if(div < LAB_COMPARE_VALUE_CONST)
00074                         return ((24389.f / 27.f * div + 16.f) / 116.f);
00075                 else
00076                         return pow(div, 1.f / 3.f);
00077         }
00078 
00079 
00080         RGB_Colour toRGB(Lab_Colour colour)
00081         {
00082                 return toRGB(colour.L, colour.a, colour.b);
00083         }
00084 
00085         RGB_Colour toRGB(float L, float a, float b)
00086         {
00087                 float vy = (L + 16.f) / 116.f;
00088                 float vx = a / 500.f + vy;
00089                 float vz = vy - b / 200.f;
00090 
00091                 float vx3 = vx * vx * vx;
00092                 float vy3 = vy * vy * vy;
00093                 float vz3 = vz * vz * vz;
00094 
00095                 if (vy3 > LAB_COMPARE_VALUE_CONST)
00096                         vy = vy3;
00097                 else
00098                         vy = (vy - 16.f / 116.f) / 7.787f;
00099 
00100                 if (vx3 > LAB_COMPARE_VALUE_CONST)
00101                         vx = vx3;
00102                 else
00103                         vx = (vx - 16.f / 116.f) / 7.787f;
00104 
00105                 if (vz3 > LAB_COMPARE_VALUE_CONST)
00106                         vz = vz3;
00107                 else
00108                         vz = (vz - 16.f / 116.f) / 7.787f;
00109 
00110                 vx *= Xn;
00111                 vz *= Zn;
00112 
00113                 float vr = vx *  3.2406f + vy * -1.5372f + vz * -0.4986f;
00114                 float vg = vx * -0.9689f + vy *  1.8758f + vz *  0.0415f;
00115                 float vb = vx *  0.0557f + vy * -0.2040f + vz *  1.0570f;
00116 
00117                 clamp(vr, 0.f, 1.f);
00118                 clamp(vg, 0.f, 1.f);
00119                 clamp(vb, 0.f, 1.f);
00120 
00121                 return RGB_Colour(GLubyte(255*vr), GLubyte(255*vg), GLubyte(255*vb));
00122         }
00123 
00124         RGB_Colour linearInterpolationRGB(float interPolBalance, RGB_Colour &start, RGB_Colour &end)
00125         {
00126                 RGB_Colour colour = RGB_Colour();
00127 
00128                 clampInterpolationValue(interPolBalance);
00129 
00130                 colour.r = (GLubyte)((1 - interPolBalance) * start.r + (interPolBalance * end.r));
00131                 colour.g = (GLubyte)((1 - interPolBalance) * start.g + (interPolBalance * end.g));
00132                 colour.b = (GLubyte)((1 - interPolBalance) * start.b + (interPolBalance * end.b));
00133 
00134                 return colour;
00135         }
00136 
00137         RGBA_Colour linearInterpolationRGBA(float interPolBalance, RGBA_Colour &start, RGBA_Colour &end)
00138         {
00139                 RGBA_Colour colour = RGBA_Colour();
00140 
00141                 clampInterpolationValue(interPolBalance);
00142 
00143                 colour.r = (GLubyte)((1 - interPolBalance) * start.r + (interPolBalance * end.r));
00144                 colour.g = (GLubyte)((1 - interPolBalance) * start.g + (interPolBalance * end.g));
00145                 colour.b = (GLubyte)((1 - interPolBalance) * start.b + (interPolBalance * end.b));
00146                 colour.a = (GLubyte)((1 - interPolBalance) * start.a + (interPolBalance * end.a));
00147 
00148                 return colour;
00149         }
00150 
00151 
00152         Lab_Colour linearInterpolationLab(float interPolBalance, Lab_Colour &start, Lab_Colour &end)
00153         {
00154                 Lab_Colour colour = Lab_Colour();
00155 
00156                 clampInterpolationValue(interPolBalance);
00157 
00158                 colour.L = (1 - interPolBalance) * start.L + (interPolBalance * end.L);
00159                 colour.a = (1 - interPolBalance) * start.a + (interPolBalance * end.a);
00160                 colour.b = (1 - interPolBalance) * start.b + (interPolBalance * end.b);
00161 
00162                 return colour;
00163         }
00164 
00165         GLubyte linearInterpolationAlpha(float interPolBalance, GLubyte startAlpha, GLubyte endAlpha)
00166         {
00167                 clampInterpolationValue(interPolBalance);
00168 
00169                 return (GLubyte)( (1 - interPolBalance) * startAlpha + (interPolBalance * endAlpha));
00170         }
00171 
00172 
00173         void clampInterpolationValue(float& interPolBalance)
00174         {
00175                 if(interPolBalance < 0.)
00176                 {
00177                         interPolBalance = 0.;
00178                         //std::cout << "Warning - Interpolation value was below 0." << std::endl;
00179                 }
00180                 else if(interPolBalance > 1.)
00181                 {
00182                         interPolBalance = 1.;
00183                         //std::cout << "Warning - Interpolation value was above 1." << std::endl;
00184                 }
00185         }
00186 }

Generated on Tue Dec 14 2010 03:52:55 for VolVis by  doxygen 1.7.2