hsl_rgb_conversions.h
Go to the documentation of this file.
00001 #ifndef HSL_RGB_CONVERSIONS_H
00002 #define HSL_RGB_CONVERSIONS_H
00003 #include<cmath>
00004 using namespace std;
00005 
00006 void extractRGB(float rgb, uint8_t& r, uint8_t& g, uint8_t& b);
00007 void extractHSL(float rgb, double& h, double& s, double& l);
00008 void writeRGB(uint8_t r, uint8_t g, uint8_t b, float& rgb);
00009 void writeHSL(double h, double s, double l, float& rgb);
00010 void HSLToRGB(double h, double s, double l, uint8_t& r, uint8_t& g, uint8_t& b);
00011 void RGBToHSL(uint8_t r, uint8_t g, uint8_t b, double& h, double& s, double& l);
00012 
00013 // H in [0, 360], S in [0, 100], L in [0, 100]
00014 // R,G,B in [0, 255] 
00015 
00016 void extractRGB(float rgb, uint8_t& r, uint8_t& g, uint8_t& b) {
00017     r = ((uint8_t*) &rgb)[2];
00018     g = ((uint8_t*) &rgb)[1];
00019     b = ((uint8_t*) &rgb)[0];
00020 }
00021 
00022 void extractHSL(float rgb, double& h, double& s, double& l) { 
00023     uint8_t r, g, b;
00024     extractRGB(rgb, r, g, b);
00025     RGBToHSL(r, g, b, h, s, l);
00026 }
00027 
00028 void writeRGB(uint8_t r, uint8_t g, uint8_t b, float& rgb) {
00029     ((uint8_t*) &rgb)[3] = 0xff;
00030     ((uint8_t*) &rgb)[2] = r;
00031     ((uint8_t*) &rgb)[1] = g;
00032     ((uint8_t*) &rgb)[0] = b;
00033 }
00034 
00035 void writeHSL(double h, double s, double l, float& rgb) {
00036     uint8_t r, g, b;
00037     HSLToRGB(h, s, l, r, g, b);
00038     writeRGB(r, g, b, rgb);
00039 }
00040 
00041 void RGBToHSL(uint8_t r, uint8_t g, uint8_t b, double& h, double& s, double& l) {
00042     double rd = r / 255.0, gd = g / 255.0, bd = b / 255.0;
00043     double min_color = min(rd, min(gd, bd));
00044     double max_color = max(rd, max(gd, bd));
00045     l = (min_color + max_color) / 2.0;
00046     if(min_color == max_color) {
00047         s = 0.0; h = 0.0;
00048         l *= 100.0;
00049         return;
00050     }
00051     if(l < 0.5) 
00052         s = (max_color - min_color) / (max_color + min_color);
00053     else
00054         s = (max_color - min_color) / (2.0 - max_color - min_color);
00055     if(rd == max_color)
00056         h = (gd - bd) / (max_color - min_color);
00057     else if(gd == max_color)
00058         h = 2.0 + (bd - rd) / (max_color - min_color);
00059     else 
00060         h = 4.0 + (rd - bd) / (max_color - min_color);
00061     h *= 60.0;
00062     if(h < 0)
00063         h += 360.0;
00064     s *= 100.0;
00065     l *= 100.0;
00066 }
00067 
00068 void HSLToRGB(double h, double s, double l, uint8_t& r, uint8_t& g, uint8_t& b) {
00069     h /= 360.0;
00070     s /= 100.0;
00071     l /= 100.0;
00072     double rd, gd, bd;
00073     if(s == 0) {
00074         rd = l; gd = l; bd = l;
00075     } else {
00076         double temp2;
00077         if(l < 0.5)
00078             temp2 = l * (1.0 + s);
00079         else
00080             temp2 = l + s - l*s;
00081         double temp1 = 2.0 * l - temp2;
00082         double rtemp3 = h + 1.0 / 3.0;
00083         if(rtemp3 < 0) rtemp3 += 1.0;
00084         if(rtemp3 > 1) rtemp3 -= 1.0;
00085         double gtemp3 = h;
00086         if(gtemp3 < 0) gtemp3 += 1.0;
00087         if(gtemp3 > 1) gtemp3 -= 1.0;
00088         double btemp3 = h - 1.0 / 3.0;
00089         if(btemp3 < 0) btemp3 += 1.0;
00090         if(btemp3 > 1) btemp3 -= 1.0;
00091         if(6.0 * rtemp3 < 1.0) rd = temp1 + (temp2 - temp1) * 6.0 * rtemp3;
00092         else if(2.0 * rtemp3 < 1.0) rd = temp2;
00093         else if(3.0 * rtemp3 < 2.0) rd = temp1 + (temp2 - temp1) * (2.0/3.0 - rtemp3) * 6.0;
00094         else rd = temp1;
00095         if(6.0 * gtemp3 < 1.0) gd = temp1 + (temp2 - temp1) * 6.0 * gtemp3;
00096         else if(2.0 * gtemp3 < 1.0) gd = temp2;
00097         else if(3.0 * gtemp3 < 2.0) gd = temp1 + (temp2 - temp1) * (2.0/3.0 - gtemp3) * 6.0;
00098         else gd = temp1;
00099         if(6.0 * btemp3 < 1.0) bd = temp1 + (temp2 - temp1) * 6.0 * btemp3;
00100         else if(2.0 * btemp3 < 1.0) bd = temp2;
00101         else if(3.0 * btemp3 < 2.0) bd = temp1 + (temp2 - temp1) * (2.0/3.0 - btemp3) * 6.0;
00102         else bd = temp1;
00103     }
00104     r = rd * 255.0;
00105     g = gd * 255.0;
00106     b = bd * 255.0;
00107 }
00108 
00109 #endif // HSL_RGB_CONVERSIONS_H


hrl_head_registration
Author(s): Kelsey Hawkins
autogenerated on Wed Nov 27 2013 11:45:27