Advertisement
Guest User

Untitled

a guest
May 6th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1.  
  2. // This is a subfunction of HSLtoRGB
  3. inline void HSLtoRGB_Subfunction(unsigned int& c, const float& temp1, const float& temp2, const float& temp3)
  4. {
  5. if((temp3 * 6) < 1)
  6. c = (unsigned int)((temp2 + (temp1 - temp2) * 6 * temp3) * 100);
  7. else if((temp3 * 2) < 1)
  8. c = (unsigned int)(temp1 * 100);
  9. else if((temp3 * 3) < 2)
  10. c = (unsigned int)((temp2 + (temp1 - temp2) * (.66666 - temp3) * 6) * 100);
  11. else
  12. c = (unsigned int)(temp2 * 100);
  13. return;
  14. }
  15.  
  16. // This function extracts the hue, saturation, and luminance from "color"
  17. // and places these values in h, s, and l respectively.
  18. inline void RGBtoHSL(unsigned int color, unsigned int& h, unsigned int& s, unsigned int& l)
  19. {
  20. unsigned int r = (unsigned int)GetRValue(color);
  21. unsigned int g = (unsigned int)GetGValue(color);
  22. unsigned int b = (unsigned int)GetBValue(color);
  23.  
  24. float r_percent = ((float)r) / 255;
  25. float g_percent = ((float)g) / 255;
  26. float b_percent = ((float)b) / 255;
  27.  
  28. float max_color = 0;
  29. if((r_percent >= g_percent) && (r_percent >= b_percent))
  30. {
  31. max_color = r_percent;
  32. }
  33. if((g_percent >= r_percent) && (g_percent >= b_percent))
  34. max_color = g_percent;
  35. if((b_percent >= r_percent) && (b_percent >= g_percent))
  36. max_color = b_percent;
  37.  
  38. float min_color = 0;
  39. if((r_percent <= g_percent) && (r_percent <= b_percent))
  40. min_color = r_percent;
  41. if((g_percent <= r_percent) && (g_percent <= b_percent))
  42. min_color = g_percent;
  43. if((b_percent <= r_percent) && (b_percent <= g_percent))
  44. min_color = b_percent;
  45.  
  46. float L = 0;
  47. float S = 0;
  48. float H = 0;
  49.  
  50. L = (max_color + min_color) / 2;
  51.  
  52. if(max_color == min_color)
  53. {
  54. S = 0;
  55. H = 0;
  56. }
  57. else
  58. {
  59. if(L < .50)
  60. {
  61. S = (max_color - min_color) / (max_color + min_color);
  62. }
  63. else
  64. {
  65. S = (max_color - min_color) / (2 - max_color - min_color);
  66. }
  67. if(max_color == r_percent)
  68. {
  69. H = (g_percent - b_percent) / (max_color - min_color);
  70. }
  71. if(max_color == g_percent)
  72. {
  73. H = 2 + (b_percent - r_percent) / (max_color - min_color);
  74. }
  75. if(max_color == b_percent)
  76. {
  77. H = 4 + (r_percent - g_percent) / (max_color - min_color);
  78. }
  79. }
  80. s = (unsigned int)(S * 100);
  81. l = (unsigned int)(L * 100);
  82. H = H * 60;
  83. if(H < 0)
  84. H += 360;
  85. h = (unsigned int)H;
  86. }
  87.  
  88. // This function converts the "color" object to the equivalent RGB values of
  89. // the hue, saturation, and luminance passed as h, s, and l respectively
  90. inline unsigned int HSLtoRGB(const unsigned int& h, const unsigned int& s, const unsigned int& l)
  91. {
  92. unsigned int r = 0;
  93. unsigned int g = 0;
  94. unsigned int b = 0;
  95.  
  96. float L = ((float)l) / 100;
  97. float S = ((float)s) / 100;
  98. float H = ((float)h) / 360;
  99.  
  100. if(s == 0)
  101. {
  102. r = l;
  103. g = l;
  104. b = l;
  105. }
  106. else
  107. {
  108. float temp1 = 0;
  109. if(L < .50)
  110. {
  111. temp1 = L * (1 + S);
  112. }
  113. else
  114. {
  115. temp1 = L + S - (L * S);
  116. }
  117.  
  118. float temp2 = 2 * L - temp1;
  119.  
  120. float temp3 = 0;
  121. for(int i = 0 ; i < 3 ; i++)
  122. {
  123. switch(i)
  124. {
  125. case 0: // red
  126. {
  127. temp3 = H + .33333f;
  128. if(temp3 > 1)
  129. temp3 -= 1;
  130. HSLtoRGB_Subfunction(r, temp1, temp2, temp3);
  131. break;
  132. }
  133. case 1: // green
  134. {
  135. temp3 = H;
  136. HSLtoRGB_Subfunction(g, temp1, temp2, temp3);
  137. break;
  138. }
  139. case 2: // blue
  140. {
  141. temp3 = H - .33333f;
  142. if(temp3 < 0)
  143. temp3 += 1;
  144. HSLtoRGB_Subfunction(b, temp1, temp2, temp3);
  145. break;
  146. }
  147. default:
  148. {
  149.  
  150. }
  151. }
  152. }
  153. }
  154. r = (unsigned int)((((float)r) / 100) * 255);
  155. g = (unsigned int)((((float)g) / 100) * 255);
  156. b = (unsigned int)((((float)b) / 100) * 255);
  157. return RGB(r, g, b);
  158. }
  159.  
  160. inline unsigned int hsl_to_rgb(unsigned int h, unsigned int s, unsigned int l)
  161. {
  162. return HSLtoRGB(h, s, l);
  163. }
  164.  
  165. inline unsigned int lighten(unsigned int color, unsigned int amount)
  166. {
  167. unsigned int h, s, l;
  168.  
  169. RGBtoHSL(color, h, s, l);
  170. l += amount;
  171. if(l > 100)
  172. {
  173. l = 100;
  174. }
  175. return HSLtoRGB(h, s, l);
  176. }
  177.  
  178. inline unsigned int darken(unsigned int color, unsigned int amount)
  179. {
  180. unsigned int h, s, l;
  181.  
  182. RGBtoHSL(color, h, s, l);
  183. if(amount >= l)
  184. {
  185. l = 0;
  186. }
  187. else
  188. {
  189. l -= amount;
  190. }
  191. return HSLtoRGB(h, s, l);
  192. }
  193.  
  194. inline unsigned int* get_lighter_colors(const unsigned int* const clr, int size, int val)
  195. {
  196. unsigned int* out = (unsigned int*) malloc(sizeof(unsigned int) * size);
  197. for(int n = 0; n < size; ++n)
  198. out[n] = lighten(clr[n], val);
  199. return out;
  200. }
  201.  
  202. inline unsigned int* get_darker_colors(const unsigned int* const clr, int size, int val)
  203. {
  204. unsigned int* out = (unsigned int*) malloc(sizeof(unsigned int) * size);
  205. for(int n = 0; n < size; ++n)
  206. out[n] = darken(clr[n], val);
  207. return out;
  208. }
  209.  
  210. inline unsigned int* make_palatte(int gian, int hue, int sat, int lum)
  211. {
  212. unsigned int* out = (unsigned int*) calloc(sizeof(int), gian);
  213.  
  214. for(int n = 0; n < gian; ++n)
  215. out[n] = hsl_to_rgb(hue + n, sat, lum);
  216. return out;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement