Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ColorHSV
- // by Matthew
- // http://forum.unity3d.com/threads/create-random-colors.12031/#post-84625
- // bugfixed and converted to C# by Deozaan
- using UnityEngine;
- // A color in HSV space
- public class ColorHSV {
- public float h { get { return _h; } set { _h = value % 360; } }
- public float s { get { return _s; } set { _s = value; } }
- public float v { get { return _v; } set { _v = value; } }
- public float a { get { return _a; } set { _a = value; } }
- public float r { get { return ToColor().r; } }
- public float g { get { return ToColor().g; } }
- public float b { get { return ToColor().b; } }
- private float _h = 0f;
- private float _s = 0f;
- private float _v = 0f;
- private float _a = 0f;
- /// <summary>Constructs a ColorHSVA with the given HSVA values</summary>
- /// <param name="hue">The hue of the color. From 0-360</param>
- /// <param name="saturation">Saturation. From 0-1, with 0 as grey</param>
- /// <param name="value">Value, AKA Brightness. From 0-1, with 0 as black</param>
- /// <param name="alpha"></param>
- public ColorHSV(float hue, float saturation = 1f, float value = 1f, float alpha = 1f) {
- _h = hue % 360;
- _s = saturation;
- _v = value;
- _a = alpha;
- }
- /// <summary>Creates a ColorHSVA from a Color</summary>
- /// <param name="color">The Color to get the initial values from</param>
- public ColorHSV(Color color) {
- float min = Mathf.Min(Mathf.Min(color.r, color.g), color.b);
- float max = Mathf.Max(Mathf.Max(color.r, color.g), color.b);
- float delta = max - min;
- // value is our max color
- _v = max;
- // saturation is percent of max
- if (!Mathf.Approximately(max, 0)) {
- _s = delta / max;
- } else {
- // all colors are zero, no saturation and hue is undefined
- _s = 0;
- _h = -1;
- return;
- }
- // grayscale image if min and max are the same
- if (Mathf.Approximately(min, max)) {
- _v = max;
- _s = 0;
- _h = -1;
- return;
- }
- // hue depends which color is max (this creates a rainbow effect)
- if (color.r == max) {
- _h = (color.g - color.b) / delta;// between yellow & magenta
- } else if (color.g == max) {
- _h = 2 + (color.b - color.r) / delta; // between cyan & yellow
- } else {
- _h = 4 + (color.r - color.g) / delta; // between magenta & cyan
- }
- // turn hue into 0-360 degrees
- _h *= 60;
- if (_h < 0) {
- _h += 360;
- }
- }
- // Return an RGBA color object
- public Color ToColor() {
- // no saturation, we can return the value across the board (grayscale)
- if (_s == 0) {
- return new Color(_v, _v, _v, _a);
- }
- // which chunk of the rainbow are we in?
- float sector = _h / 60;
- // split across the decimal (ie 3.87 into 3 and 0.87)
- int i;
- i = Mathf.FloorToInt(sector);
- float f = sector - i;
- float v = _v;
- float p = v * (1 - _s);
- float q = v * (1 - _s * f);
- float t = v * (1 - _s * (1 - f));
- // build our rgb color
- Color color = new Color(0, 0, 0, _a);
- switch (i) {
- case 0:
- color.r = v;
- color.g = t;
- color.b = p;
- break;
- case 1:
- color.r = q;
- color.g = v;
- color.b = p;
- break;
- case 2:
- color.r = p;
- color.g = v;
- color.b = t;
- break;
- case 3:
- color.r = p;
- color.g = q;
- color.b = v;
- break;
- case 4:
- color.r = t;
- color.g = p;
- color.b = v;
- break;
- default:
- color.r = v;
- color.g = p;
- color.b = q;
- break;
- }
- return color;
- }
- // Format nicely
- new public string ToString() {
- return string.Format("h: {0:0.00}, s: {1:0.00}, v: {2:0.00}, a: {3:0.00}", _h, _s, _v, _a);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement