Advertisement
Deozaan

ColorHSV.cs

Mar 9th, 2015
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. // ColorHSV
  2. // by Matthew
  3. // http://forum.unity3d.com/threads/create-random-colors.12031/#post-84625
  4. // bugfixed and converted to C# by Deozaan
  5.  
  6. using UnityEngine;
  7.  
  8. // A color in HSV space
  9. public class ColorHSV {
  10.  
  11.     public float h { get { return _h; } set { _h = value % 360; } }
  12.     public float s { get { return _s; } set { _s = value; } }
  13.     public float v { get { return _v; } set { _v = value; } }
  14.     public float a { get { return _a; } set { _a = value; } }
  15.  
  16.     public float r { get { return ToColor().r; } }
  17.     public float g { get { return ToColor().g; } }
  18.     public float b { get { return ToColor().b; } }
  19.  
  20.     private float _h = 0f;
  21.     private float _s = 0f;
  22.     private float _v = 0f;
  23.     private float _a = 0f;
  24.  
  25.     /// <summary>Constructs a ColorHSVA with the given HSVA values</summary>
  26.     /// <param name="hue">The hue of the color. From 0-360</param>
  27.     /// <param name="saturation">Saturation. From 0-1, with 0 as grey</param>
  28.     /// <param name="value">Value, AKA Brightness. From 0-1, with 0 as black</param>
  29.     /// <param name="alpha"></param>
  30.     public ColorHSV(float hue, float saturation = 1f, float value = 1f, float alpha = 1f) {
  31.         _h = hue % 360;
  32.         _s = saturation;
  33.         _v = value;
  34.         _a = alpha;
  35.     }
  36.  
  37.  
  38.     /// <summary>Creates a ColorHSVA from a Color</summary>
  39.     /// <param name="color">The Color to get the initial values from</param>
  40.     public ColorHSV(Color color) {
  41.         float min = Mathf.Min(Mathf.Min(color.r, color.g), color.b);
  42.         float max = Mathf.Max(Mathf.Max(color.r, color.g), color.b);
  43.         float delta = max - min;
  44.  
  45.         // value is our max color
  46.         _v = max;
  47.  
  48.         // saturation is percent of max
  49.         if (!Mathf.Approximately(max, 0)) {
  50.             _s = delta / max;
  51.         } else {
  52.             // all colors are zero, no saturation and hue is undefined
  53.             _s = 0;
  54.             _h = -1;
  55.             return;
  56.         }
  57.  
  58.         // grayscale image if min and max are the same
  59.         if (Mathf.Approximately(min, max)) {
  60.             _v = max;
  61.             _s = 0;
  62.             _h = -1;
  63.             return;
  64.         }
  65.  
  66.         // hue depends which color is max (this creates a rainbow effect)
  67.         if (color.r == max) {
  68.             _h = (color.g - color.b) / delta;// between yellow & magenta
  69.         } else if (color.g == max) {
  70.             _h = 2 + (color.b - color.r) / delta; // between cyan & yellow
  71.         } else {
  72.             _h = 4 + (color.r - color.g) / delta; // between magenta & cyan
  73.         }
  74.  
  75.         // turn hue into 0-360 degrees
  76.         _h *= 60;
  77.         if (_h < 0) {
  78.             _h += 360;
  79.         }
  80.     }
  81.  
  82.     // Return an RGBA color object
  83.     public Color ToColor() {
  84.         // no saturation, we can return the value across the board (grayscale)
  85.         if (_s == 0) {
  86.             return new Color(_v, _v, _v, _a);
  87.         }
  88.  
  89.         // which chunk of the rainbow are we in?
  90.         float sector = _h / 60;
  91.         // split across the decimal (ie 3.87 into 3 and 0.87)
  92.         int i;
  93.         i = Mathf.FloorToInt(sector);
  94.         float f = sector - i;
  95.  
  96.         float v = _v;
  97.         float p = v * (1 - _s);
  98.         float q = v * (1 - _s * f);
  99.         float t = v * (1 - _s * (1 - f));
  100.  
  101.         // build our rgb color
  102.         Color color = new Color(0, 0, 0, _a);
  103.  
  104.         switch (i) {
  105.             case 0:
  106.                 color.r = v;
  107.                 color.g = t;
  108.                 color.b = p;
  109.                 break;
  110.             case 1:
  111.                 color.r = q;
  112.                 color.g = v;
  113.                 color.b = p;
  114.                 break;
  115.             case 2:
  116.                 color.r = p;
  117.                 color.g = v;
  118.                 color.b = t;
  119.                 break;
  120.             case 3:
  121.                 color.r = p;
  122.                 color.g = q;
  123.                 color.b = v;
  124.                 break;
  125.             case 4:
  126.                 color.r = t;
  127.                 color.g = p;
  128.                 color.b = v;
  129.                 break;
  130.             default:
  131.                 color.r = v;
  132.                 color.g = p;
  133.                 color.b = q;
  134.                 break;
  135.         }
  136.  
  137.         return color;
  138.     }
  139.  
  140.     // Format nicely
  141.     new public string ToString() {
  142.         return string.Format("h: {0:0.00}, s: {1:0.00}, v: {2:0.00}, a: {3:0.00}", _h, _s, _v, _a);
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement