/** * Converts an RGB color value to HSV. Conversion formula * adapted from http://en.wikipedia.org/wiki/HSV_color_space. * Assumes r, g, and b are contained in the set [0, 255] and * returns h, s, and v in the set [0, 1]. * * @param Number r The red color value * @param Number g The green color value * @param Number b The blue color value * @return Array The HSV representation */ void RGBConverter::rgbToHsv(byte r, byte g, byte b, double hsv[]) { double rd = (double) r/255; double gd = (double) g/255; double bd = (double) b/255; double max = threeway_max(rd, gd, bd), min = threeway_min(rd, gd, bd); double h, s, v = max; double d = max - min; s = max == 0 ? 0 : d / max; if (max == min) { h = 0; // achromatic } else { if (max == rd) { h = (gd - bd) / d + (gd < bd ? 6 : 0); } else if (max == gd) { h = (bd - rd) / d + 2; } else if (max == bd) { h = (rd - gd) / d + 4; } h /= 6; } hsv[0] = h; hsv[1] = s; hsv[2] = v; }
// This function is by Robert Atkins // https://github.com/ratkins/RGBConverter void ClydeDev::rgb2hsv(uint8_t r, uint8_t g, uint8_t b, double *hsv) { double rd = (double) r/255; double gd = (double) g/255; double bd = (double) b/255; double max = threeway_max(rd, gd, bd), min = threeway_min(rd, gd, bd); double h, s, v = max; double d = max - min; s = max == 0 ? 0 : d / max; if (max == min) { h = 0; // achromatic } else { if (max == rd) { h = (gd - bd) / d + (gd < bd ? 6 : 0); } else if (max == gd) { h = (bd - rd) / d + 2; } else if (max == bd) { h = (rd - gd) / d + 4; } h /= 6; } hsv[0] = h; hsv[1] = s; hsv[2] = v; }
/** * Converts an RGB color value to HSL. Conversion formula * adapted from http://en.wikipedia.org/wiki/HSL_color_space. * Assumes r, g, and b are contained in the set [0, 255] and * returns h, s, and l in the set [0, 1]. * * @param Number r The red color value * @param Number g The green color value * @param Number b The blue color value * @return Array The HSL representation */ void RGBConverter::rgbToHsl(byte r, byte g, byte b, double hsl[]) { double rd = (double) r/255; double gd = (double) g/255; double bd = (double) b/255; double max = threeway_max(rd, gd, bd); double min = threeway_min(rd, gd, bd); double h, s, l = (max + min) / 2; if (max == min) { h = s = 0; // achromatic } else { double d = max - min; s = l > 0.5 ? d / (2 - max - min) : d / (max + min); if (max == rd) { h = (gd - bd) / d + (gd < bd ? 6 : 0); } else if (max == gd) { h = (bd - rd) / d + 2; } else if (max == bd) { h = (rd - gd) / d + 4; } h /= 6; } hsl[0] = h; hsl[1] = s; hsl[2] = l; }