Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by pdunton · Apr 14, 2014 at 11:38 PM · javascriptcolorcolorpicker

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();
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Apr 15, 2014 at 12:28 AM 0
Share

http://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB

avatar image pdunton · Apr 19, 2014 at 08:54 PM 0
Share

Im sorry, I don't really understand how to make that into a script. Thank you though!

avatar image Graham-Dunnett ♦♦ · Apr 19, 2014 at 09:11 PM 1
Share

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. ;-)

avatar image Eric5h5 · Apr 19, 2014 at 09:23 PM 1
Share

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....)

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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); 
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pdunton · Apr 23, 2014 at 03:21 AM 0
Share

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!

avatar image drudiverse · Jun 18, 2014 at 03:52 AM 0
Share

nice work thanks, you can add h=h%1; s=s%1; v=v%1;

at top of fucntion to be safe.

avatar image
0

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);
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by pankajb · Apr 26, 2016 at 04:30 AM

Color.HSVToRGB

http://docs.unity3d.com/ScriptReference/Color.HSVToRGB.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges