- Home /
 
Runtime Equivelent of EditorGUIUtility.HSVToRGB()
Apperently after about a month of work on this game I have, I hit build and it says that I can't use EditorGUIUtility.HSVToRGB. So now, a couple of scripts in my game don't work. In the first script, HueShift, I use HSVToRGB() to shift the hue to many color. Pretty simple except now that I cant use HSVToRGB() I need a similar set of code that does the same thing. And in the second one, CoinCreate, I use HSVToRGB() to pick a random (Nice looking) color. Again, all I'm looking for is something that works exactly like HSVToRGB(). Thats it. Thanks!
HueShift:
 #pragma strict
 var h : float = 0.0;
 function Start () {
 
 }
 
 function Update () {
 
     gameObject.renderer.material.color = EditorGUIUtility.HSVToRGB(h, 1, 1);
     if(h < 1){
         h = h + 0.001;
     } else {
         h = 0;
     }
 
 }
 
               CoinCreater (Look at lines 29-33):
 #pragma strict
 var xpos : float;
 var spawnpoint : Vector3;
 var coin : GameObject;
 var rotation : Quaternion;
 rotation.eulerAngles = new Vector3(0, 0, 0);
 var newCoin : GameObject;
 var rate : float = 3.0;
 var fasterRate : float = 0.0;
 var scriptEnabled : boolean = true;
 
 function Start () {
 Invoke("CreateCoin", rate);
 }
 
 function Update () {
 
 }
 
 function setRate(newRate : float){
     rate = newRate;
 }
 
 function CreateCoin(){
     fasterRate = 0.05;
     xpos = Random.Range(-50.0,50.0);
     spawnpoint = new Vector3(xpos, 80, 0);
     
     var h : float = Random.Range(0.0,1.0);
     if(scriptEnabled){
         newCoin = Instantiate(coin, spawnpoint, rotation);
         newCoin.renderer.material.color = EditorGUIUtility.HSVToRGB(h, 1, 1);
     }
     if(rate > 0.4){
         rate = rate - fasterRate;
     }
     if(scriptEnabled){
         Invoke("CreateCoin", rate);
     }
 }
 
 function Unpause(){
     yield WaitForSeconds(rate);
     CreateCoin();
 }
 
              Im sorry, I don't really understand how to make that into a script. Thank you though!
When I googled "code to convert hsv to rgb" and took the first hit, I get to:
http://www.cs.rit.edu/~ncs/color/t_convert.html
Pro tip 1: Google is awesome. ;-) Pro tip 2: Do a build 100 times a day. ;-)
When I googled "unity convert hsb" and took the first hit, I get to:
http://wiki.unity3d.com/index.php?title=HSBColor
(Well, technically the first hit was a forum topic which had a link to that page, but nevertheless that's where I got to....)
Answer by Gabigabigo · Apr 21, 2014 at 05:36 PM
Based on a JS code (http://stackoverflow.com/questions/17242144/javascript-convert-hsb-hsv-color-to-rgb-accurately), I created this function (Unity Script) :
 function HSVToRGB(h : float, s : float, v : float) {
     var r : float;
     var g : float;
     var b : float;
     var i : float;
     var f : float;
     var p : float;
     var q : float;
     var t : float; 
     i = Mathf.Floor(h * 6);
     f = h * 6 - i;
     p = v * (1 - s);
     q = v * (1 - f * s);
     t = v * (1 - (1 - f) * s);
     switch (i % 6) {
         case 0: r = v; g = t; b = p; break;
         case 1: r = q; g = v; b = p; break;
         case 2: r = p; g = v; b = t; break;
         case 3: r = p; g = q; b = v; break;
         case 4: r = t; g = p; b = v; break;
         case 5: r = v; g = p; b = q; break;
     }
     return Color(r,g,b); 
 }
 
              Thanks a lot!!! I put this in its own script called HSVToRGB, and now ins$$anonymous$$d of EditorGUIUtility.HSVToRGB() I use HSVToRGB.HSVToRGB(). Thanks!
nice work thanks, you can add h=h%1; s=s%1; v=v%1;
at top of fucntion to be safe.
Answer by Audiocurve · Dec 26, 2015 at 11:14 AM
Thanks for posting this. Very useful. Here is the C# version:
     public Color HSVToRGB(float h, float s, float v)
     {
         h = h % 1;
         s = s % 1;
         v = v % 1;
 
         float r = 0;
         float g = 0;
         float b = 0;
         float f, p, q, t;
         int i;
         i = (int) Mathf.Floor(h * 6);
         f = h * 6 - i;
         p = v * (1 - s);
         q = v * (1 - f * s);
         t = v * (1 - (1 - f) * s);
         switch (i % 6)
         {
             case 0: r = v; g = t; b = p; break;
             case 1: r = q; g = v; b = p; break;
             case 2: r = p; g = v; b = t; break;
             case 3: r = p; g = q; b = v; break;
             case 4: r = t; g = p; b = v; break;
             case 5: r = v; g = p; b = q; break;
         }
         return new Color(r, g, b);
     }
 
              Your answer
 
             Follow this Question
Related Questions
I am trying to make this color picker script work 2 Answers
Get "on screen" color of a texture to use in a legend 2 Answers
Eyedropper/colordropper GameObject 0 Answers
How to loop through colors 2 Answers
Help,How to fix this? 0 Answers