Advertisement
Deozaan

HSBColor.cs - Unity

Feb 19th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. /*
  2.  * http://wiki.unity3d.com/index.php/HSBColor
  3.  * © Fri Jul  7 19:39:47 CDT 2006 Graveck Interactive
  4.  * by Jonathan Czeck
  5.  */
  6.  
  7. using UnityEngine;
  8.  
  9. [System.Serializable]
  10. public struct HSBColor {
  11.     public float h;
  12.     public float s;
  13.     public float b;
  14.     public float a;
  15.  
  16.     public HSBColor(float h, float s, float b, float a) {
  17.         this.h = h;
  18.         this.s = s;
  19.         this.b = b;
  20.         this.a = a;
  21.     }
  22.  
  23.     public HSBColor(float h, float s, float b) {
  24.         this.h = h;
  25.         this.s = s;
  26.         this.b = b;
  27.         this.a = 1f;
  28.     }
  29.  
  30.     public HSBColor(Color col) {
  31.         HSBColor temp = FromColor(col);
  32.         h = temp.h;
  33.         s = temp.s;
  34.         b = temp.b;
  35.         a = temp.a;
  36.     }
  37.  
  38.     public static HSBColor FromColor(Color color) {
  39.         HSBColor ret = new HSBColor(0f, 0f, 0f, color.a);
  40.  
  41.         float r = color.r;
  42.         float g = color.g;
  43.         float b = color.b;
  44.  
  45.         float max = Mathf.Max(r, Mathf.Max(g, b));
  46.  
  47.         if (max <= 0) {
  48.             return ret;
  49.         }
  50.  
  51.         float min = Mathf.Min(r, Mathf.Min(g, b));
  52.         float dif = max - min;
  53.  
  54.         if (max > min) {
  55.             if (g == max) {
  56.                 ret.h = (b - r) / dif * 60f + 120f;
  57.             } else if (b == max) {
  58.                 ret.h = (r - g) / dif * 60f + 240f;
  59.             } else if (b > g) {
  60.                 ret.h = (g - b) / dif * 60f + 360f;
  61.             } else {
  62.                 ret.h = (g - b) / dif * 60f;
  63.             }
  64.             if (ret.h < 0) {
  65.                 ret.h = ret.h + 360f;
  66.             }
  67.         } else {
  68.             ret.h = 0;
  69.         }
  70.  
  71.         ret.h *= 1f / 360f;
  72.         ret.s = (dif / max) * 1f;
  73.         ret.b = max;
  74.  
  75.         return ret;
  76.     }
  77.  
  78.     public static Color ToColor(HSBColor hsbColor) {
  79.         float r = hsbColor.b;
  80.         float g = hsbColor.b;
  81.         float b = hsbColor.b;
  82.         if (hsbColor.s != 0) {
  83.             float max = hsbColor.b;
  84.             float dif = hsbColor.b * hsbColor.s;
  85.             float min = hsbColor.b - dif;
  86.  
  87.             float h = hsbColor.h * 360f;
  88.  
  89.             if (h < 60f) {
  90.                 r = max;
  91.                 g = h * dif / 60f + min;
  92.                 b = min;
  93.             } else if (h < 120f) {
  94.                 r = -(h - 120f) * dif / 60f + min;
  95.                 g = max;
  96.                 b = min;
  97.             } else if (h < 180f) {
  98.                 r = min;
  99.                 g = max;
  100.                 b = (h - 120f) * dif / 60f + min;
  101.             } else if (h < 240f) {
  102.                 r = min;
  103.                 g = -(h - 240f) * dif / 60f + min;
  104.                 b = max;
  105.             } else if (h < 300f) {
  106.                 r = (h - 240f) * dif / 60f + min;
  107.                 g = min;
  108.                 b = max;
  109.             } else if (h <= 360f) {
  110.                 r = max;
  111.                 g = min;
  112.                 b = -(h - 360f) * dif / 60 + min;
  113.             } else {
  114.                 r = 0;
  115.                 g = 0;
  116.                 b = 0;
  117.             }
  118.         }
  119.  
  120.         return new Color(Mathf.Clamp01(r), Mathf.Clamp01(g), Mathf.Clamp01(b), hsbColor.a);
  121.     }
  122.  
  123.     public Color ToColor() {
  124.         return ToColor(this);
  125.     }
  126.  
  127.     public override string ToString() {
  128.         return "H:" + h + " S:" + s + " B:" + b;
  129.     }
  130.  
  131.     public static Color Slerp(Color a, Color b, float t) {
  132.         return (Lerp(FromColor(a), FromColor(b), t)).ToColor();
  133.     }
  134.  
  135.     public static HSBColor Lerp(HSBColor a, HSBColor b, float t) {
  136.         float h, s;
  137.  
  138.         //check special case black (color.b==0): interpolate neither hue nor saturation!
  139.         //check special case grey (color.s==0): don't interpolate hue!
  140.         if (a.b == 0) {
  141.             h = b.h;
  142.             s = b.s;
  143.         } else if (b.b == 0) {
  144.             h = a.h;
  145.             s = a.s;
  146.         } else {
  147.             if (a.s == 0) {
  148.                 h = b.h;
  149.             } else if (b.s == 0) {
  150.                 h = a.h;
  151.             } else {
  152.                 // works around bug with LerpAngle
  153.                 float angle = Mathf.LerpAngle(a.h * 360f, b.h * 360f, t);
  154.                 while (angle < 0f)
  155.                     angle += 360f;
  156.                 while (angle > 360f)
  157.                     angle -= 360f;
  158.                 h = angle / 360f;
  159.             }
  160.             s = Mathf.Lerp(a.s, b.s, t);
  161.         }
  162.         return new HSBColor(h, s, Mathf.Lerp(a.b, b.b, t), Mathf.Lerp(a.a, b.a, t));
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement