Advertisement
JennyMcJenster

Standard | 0-1 Rainbow Function

Sep 8th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. /*
  2.     A function that outputs an RGBA colour when given a value between 0 and 1
  3.      (inc/decrementing the value for "cycle" passed will cycle through the rainbow)
  4.     You can also define a range to which the output colour is scaled and clamped
  5.      (useful if you want to cycle through dark/pastel/greyed rainbow colours instead of pure, e.g.)
  6.        minVal | maxVal | RESULT
  7.        0.0    | 256.0  | Colours are fully saturated
  8.        128.0  | 256.0  | Colours are semi-saturated into white; pastel colours
  9.        0.0    | 128.0  | Colours are semi-saturated into black; darkened colours
  10.        96.0   | 144.0  | Colours are distinctly greyed
  11.        256.0  | 256.0  | Colour will just be white, what are you doing
  12.        0.0    | 0.0    | Now it's just gone completely black, stop this nonsense
  13.        128.0  | 128.0  | Something tells me you're not doing this properly
  14. */
  15.  
  16. // Need "math.h" for math functions ("cmath" should also be fine, but blegh)
  17. #include <math.h>
  18.  
  19. // Placeholder colour structure
  20. struct Colour
  21. {
  22.     Colour():r(255),g(255),b(255),a(255){};
  23.     Colour(int r,int g,int b,int a=255):r(r),g(g),b(b),a(a){};
  24.     int r; int g; int b; int a;
  25. }
  26.  
  27. Colour Rainbow(double cycle, int alpha=255, double minVal=0.0, double maxVal=255.0)
  28. // "alpha" is alpha of output colour,
  29. // "minVal" and "maxVal" define a range to which the output colour is scaled and clamped
  30. {
  31.     // ensure that "cycle" is rotated to a value within [0 <= cycle < 1]
  32.     if(cycle<0.0||cycle>=1.0) cycle=cycle-floor(cycle);
  33.     // ensure that "alpha" is a value within [0 <= alpha <= 255]
  34.     alpha=(alpha<0?0:(alpha>255?255:alpha));
  35.     // ensure that "minVal" is the smallest of itself and "maxVal"
  36.     if(minVal>maxVal){ minVal=minVal-maxVal; maxVal=maxVal+minVal; minVal=maxVal-minVal; }
  37.     // ensure that "minVal" is [>=0] and "maxVal" is [<=255]
  38.     minVal=(minVal<0?0:minVal); maxVal=(maxVal>255?255:maxVal);
  39.    
  40.     // get each of r, g and b given the current value of "cycle"
  41.     double r = (maxVal-minVal)*(abs((1.0/2.0 - c)*6.0) - 1.0)+minVal;
  42.     double g = (maxVal-minVal)*(2.0 - abs((1.0/3.0 - c)*6.0))+minVal;
  43.     double b = (maxVal-minVal)*(2.0 - abs((2.0/3.0 - c)*6.0))+minVal;
  44.     // ensure they're clamped between the minimum and maximum values
  45.     r = (r<minVal?minVal:(r>maxVal?maxVal:r));
  46.     g = (g<minVal?minVal:(g>maxVal?maxVal:g));
  47.     b = (b<minVal?minVal:(b>maxVal?maxVal:b));
  48.    
  49.     // To output for another format, rewrite this line and change the return type as necessary
  50.     return Colour( static_cast<int>(r), static_cast<int>(g), static_cast<int>(b), alpha );
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement