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 void extractRGB(float rgb, uint8_t& r, uint8_t& g, uint8_t& b) {
00014     r = ((uint8_t*) &rgb)[2];
00015     g = ((uint8_t*) &rgb)[1];
00016     b = ((uint8_t*) &rgb)[0];
00017 }
00018 
00019 void extractHSL(float rgb, double& h, double& s, double& l) { 
00020     uint8_t r, g, b;
00021     extractRGB(rgb, r, g, b);
00022     RGBToHSL(r, g, b, h, s, l);
00023 }
00024 
00025 void writeRGB(uint8_t r, uint8_t g, uint8_t b, float& rgb) {
00026     ((uint8_t*) &rgb)[3] = 0xff;
00027     ((uint8_t*) &rgb)[2] = r;
00028     ((uint8_t*) &rgb)[1] = g;
00029     ((uint8_t*) &rgb)[0] = b;
00030 }
00031 
00032 void writeHSL(double h, double s, double l, float& rgb) {
00033     uint8_t r, g, b;
00034     HSLToRGB(h, s, l, r, g, b);
00035     writeRGB(r, g, b, rgb);
00036 }
00037 
00038 void RGBToHSL(uint8_t r, uint8_t g, uint8_t b, double& h, double& s, double& l) {
00039     double rd = r / 255.0, gd = g / 255.0, bd = b / 255.0;
00040     double min_color = min(rd, min(gd, bd));
00041     double max_color = max(rd, max(gd, bd));
00042     l = (min_color + max_color) / 2.0;
00043     if(min_color == max_color) {
00044         s = 0.0; h = 0.0;
00045         l *= 100.0;
00046         return;
00047     }
00048     if(l < 0.5) 
00049         s = (max_color - min_color) / (max_color + min_color);
00050     else
00051         s = (max_color - min_color) / (2.0 - max_color - min_color);
00052     if(rd == max_color)
00053         h = (gd - bd) / (max_color - min_color);
00054     else if(gd == max_color)
00055         h = 2.0 + (bd - rd) / (max_color - min_color);
00056     else 
00057         h = 4.0 + (rd - bd) / (max_color - min_color);
00058     h *= 60.0;
00059     if(h < 0)
00060         h += 360.0;
00061     s *= 100.0;
00062     l *= 100.0;
00063 }
00064 
00065 void HSLToRGB(double h, double s, double l, uint8_t& r, uint8_t& g, uint8_t& b) {
00066     h /= 360.0;
00067     s /= 100.0;
00068     l /= 100.0;
00069     double rd, gd, bd;
00070     if(s == 0) {
00071         rd = l; gd = l; bd = l;
00072     } else {
00073         double temp2;
00074         if(l < 0.5)
00075             temp2 = l * (1.0 + s);
00076         else
00077             temp2 = l + s - l*s;
00078         double temp1 = 2.0 * l - temp2;
00079         double rtemp3 = h + 1.0 / 3.0;
00080         if(rtemp3 < 0) rtemp3 += 1.0;
00081         if(rtemp3 > 1) rtemp3 -= 1.0;
00082         double gtemp3 = h;
00083         if(gtemp3 < 0) gtemp3 += 1.0;
00084         if(gtemp3 > 1) gtemp3 -= 1.0;
00085         double btemp3 = h - 1.0 / 3.0;
00086         if(btemp3 < 0) btemp3 += 1.0;
00087         if(btemp3 > 1) btemp3 -= 1.0;
00088         if(6.0 * rtemp3 < 1.0) rd = temp1 + (temp2 - temp1) * 6.0 * rtemp3;
00089         else if(2.0 * rtemp3 < 1.0) rd = temp2;
00090         else if(3.0 * rtemp3 < 2.0) rd = temp1 + (temp2 - temp1) * (2.0/3.0 - rtemp3) * 6.0;
00091         else rd = temp1;
00092         if(6.0 * gtemp3 < 1.0) gd = temp1 + (temp2 - temp1) * 6.0 * gtemp3;
00093         else if(2.0 * gtemp3 < 1.0) gd = temp2;
00094         else if(3.0 * gtemp3 < 2.0) gd = temp1 + (temp2 - temp1) * (2.0/3.0 - gtemp3) * 6.0;
00095         else gd = temp1;
00096         if(6.0 * btemp3 < 1.0) bd = temp1 + (temp2 - temp1) * 6.0 * btemp3;
00097         else if(2.0 * btemp3 < 1.0) bd = temp2;
00098         else if(3.0 * btemp3 < 2.0) bd = temp1 + (temp2 - temp1) * (2.0/3.0 - btemp3) * 6.0;
00099         else bd = temp1;
00100     }
00101     r = rd * 255.0;
00102     g = gd * 255.0;
00103     b = bd * 255.0;
00104 }
00105 
00106 #endif // HSL_RGB_CONVERSIONS_H


hrl_phri_2011
Author(s): Kelsey Hawkins
autogenerated on Wed Nov 27 2013 12:22:40