Search Unity

create random colors

Discussion in 'Scripting' started by wimeck, Jul 18, 2008.

  1. wimeck

    wimeck

    Joined:
    May 26, 2008
    Posts:
    50
    Hi there,

    I have a question. I want to make a "gun" that shoots out cubes with random colors. So for example, the 'red' value of a color should be a random number inbetween 0 and 255 every time you shoot. Here is the script till now:

    ---------------------------------

    var projectile : Rigidbody;
    var speed = 20;
    var angle = 3;
    var fireRate = 0.5;

    private var nextFire = 0.0;


    function Update () {
    if (Input.GetButton ("Fire1") Time.time > nextFire) {
    nextFire = Time.time + fireRate;
    var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position,
    transform.rotation);
    instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,angle,speed));
    Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
    }
    }

    ---------------------------------

    Now I just apply a material with a color.

    Thanks in advance!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    renderer.material.color = Color(Random.Range(0.0,1.0),Random.Range(0.0,1.0),Random.Range(0.0,1.0));

    It's worth noting that most colors are ugly. You'll do better picking colors from a list of colors.
     
  3. wimeck

    wimeck

    Joined:
    May 26, 2008
    Posts:
    50
    Thanks a lot! Well, right now I'm just trying to learn how to program. I'm just curious how you can do something like this, I'll worry about aesthetics later:).
     
  4. wimeck

    wimeck

    Joined:
    May 26, 2008
    Posts:
    50
    And another question. If I put your code inside function Update (), it keeps on changing color at a very rapid speed, which gives an interesting effect too. Is there a way to control the speed of the color changing, to make it animate in a slower pace?
    Thanks again!
     
  5. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    :D

    it's possible to script out the RGB combinations that you personally despise, but yeah, it's probably better to choose from an array or something.
     
  6. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    Use a texture to get colors..

    .org
     
  7. wimeck

    wimeck

    Joined:
    May 26, 2008
    Posts:
    50
    Well, I would like to quickly change between ranges of colors, like random colors between red-black, blue-black, green-black etcetera. That would be a lot of work with lists, or plain textures/colors. And again, I also just want to learn how to do it...
     
  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I don't think so. Omar might be onto something better with the texture idea, but I would just add the following array to the object to which your prior script was attached, set the length of the array in the inspector, and choose some pretty ones using the system color picker:

    Code (csharp):
    1. var colorChoices : Color[];
    Then, add this line (making sure you have a Material attached to the prefab you are using as a "projectile"):

    Code (csharp):
    1. instantiatedProjectile.renderer.material.color = colorChoices[Random.Range(0, (colorChoices.length)) ];
    You can set up multiple arrays, if you want to switch between color themes. There's no end to the control you could exert, but hopefully this can get you started.
     
    TechSupportIncoming1 likes this.
  9. wimeck

    wimeck

    Joined:
    May 26, 2008
    Posts:
    50
    Thanks, I'll have a look at it!
     
  10. housewarmer

    housewarmer

    Joined:
    Apr 18, 2008
    Posts:
    74
    I played around with the texture method and it worked quite well. In my case I just used a 8x1 pixel image with each pixel being a different color, then used GetPixel (with RandomRange applied to the x value) to specify the material color.

    The benefit here is that you can set up a tailored color index both visually and quickly.
     
    Xform likes this.
  11. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    That's very cool, housewarmer. I wasn't even aware that we had the kind of control that is available in the Texture2D Class. I think if I were going to create the colors procedurally, I would still use the array method, so I could look at them in the inspector, but I will definitely not use the hand-picking method I mentioned above now. For fast eyeballs-based choosing, your method sounds great.

    Now, even though you can create .png's with that Class, I don't see any options to actually use a .png in-game (it's either DXT or nothing - no lossless stuff). Does this mean that there is no way to compress the textures you would create outside of Unity, for this task, or does the LZMA take care of that upon building? If that doesn't happen, would an array of colors even get LZMA-compressed?
     
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, LZMA compresses everything. (As long as you're making a web build of course.)

    --Eric
     
  13. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Are only web builds compressed? It's certainly more important there, but fast download times and bandwidth savings are good too.
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Widget builds are compressed too, but they're essentially web builds anyway.

    Well, you can always make standard .zip files or compressed .dmg files for standalones. Granted the compression for those isn't as good as LZMA, which doesn't seem to be in widespread use.

    --Eric
     
  15. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    The HSV color space is much easier if you want to work with random colors. You can specify you want full saturation and value, and just randomize the hue. This results in a bright randomized colors. If you randomize in RGB you basically get a lot of muddy and washed out browns.

    Code (csharp):
    1.  
    2. /**
    3. * A color in HSV space
    4. */
    5. class ColorHSV extends System.Object
    6. {
    7.     var h:float = 0.0;
    8.     var s:float = 0.0;
    9.     var v:float = 0.0;
    10.     var a:float = 0.0;
    11.    
    12.     /**
    13.     * Construct without alpha (which defaults to 1)
    14.     */
    15.     function ColorHSV(h:float, s:float, v:float)
    16.     {
    17.         this.h = h;
    18.         this.s = s;
    19.         this.v = v;
    20.         this.a = 1.0;
    21.     }
    22.    
    23.     /**
    24.     * Construct with alpha
    25.     */
    26.     function ColorHSV(h:float, s:float, v:float, a:float)
    27.     {
    28.         this.h = h;
    29.         this.s = s;
    30.         this.v = v;
    31.         this.a = a;
    32.     }
    33.    
    34.     /**
    35.     * Create from an RGBA color object
    36.     */
    37.     function ColorHSV(color:Color)
    38.     {
    39.         var min:float = Mathf.Min(Mathf.Min(color.r, color.g), color.b);
    40.         var max:float = Mathf.Max(Mathf.Max(color.r, color.g), color.b);
    41.         var delta:float = max - min;
    42.  
    43.         // value is our max color
    44.         this.v = max;
    45.  
    46.         // saturation is percent of max
    47.         if(!Mathf.Approximately(max, 0))
    48.             this.s = delta / max;
    49.         else
    50.         {
    51.             // all colors are zero, no saturation and hue is undefined
    52.             this.s = 0;
    53.             this.h = -1;
    54.             return;
    55.         }
    56.  
    57.         // grayscale image if min and max are the same
    58.         if(Mathf.Approximately(min, max))
    59.         {
    60.             this.v = max;
    61.             this.s = 0;
    62.             this.h = -1;
    63.             return;
    64.         }
    65.        
    66.         // hue depends which color is max (this creates a rainbow effect)
    67.         if( color.r == max )
    68.             this.h = ( color.g - color.b ) / delta;         // between yellow  magenta
    69.         else if( color.g == max )
    70.             this.h = 2 + ( color.b - color.r ) / delta; // between cyan  yellow
    71.         else
    72.             this.h = 4 + ( color.r - color.g ) / delta; // between magenta  cyan
    73.  
    74.         // turn hue into 0-360 degrees
    75.         this.h *= 60;
    76.         if(this.h < 0 )
    77.             this.h += 360;
    78.     }
    79.    
    80.     /**
    81.     * Return an RGBA color object
    82.     */
    83.     function ToColor():Color
    84.     {
    85.         // no saturation, we can return the value across the board (grayscale)
    86.         if(this.s == 0 )
    87.             return new Color(this.v, this.v, this.v, this.a);
    88.  
    89.         // which chunk of the rainbow are we in?
    90.         var sector:float = this.h / 60;
    91.        
    92.         // split across the decimal (ie 3.87 into 3 and 0.87)
    93.         var i:int; i = Mathf.Floor(sector);
    94.         var f:float = sector - i;
    95.        
    96.         var v:float = this.v;
    97.         var p:float = v * ( 1 - s );
    98.         var q:float = v * ( 1 - s * f );
    99.         var t:float = v * ( 1 - s * ( 1 - f ) );
    100.        
    101.         // build our rgb color
    102.         var color:Color = new Color(0, 0, 0, this.a);
    103.        
    104.         switch(i)
    105.         {
    106.             case 0:
    107.                 color.r = v;
    108.                 color.g = t;
    109.                 color.b = p;
    110.                 break;
    111.             case 1:
    112.                 color.r = q;
    113.                 color.g = v;
    114.                 color.b = p;
    115.                 break;
    116.             case 2:
    117.                 color.r  = p;
    118.                 color.g  = v;
    119.                 color.b  = t;
    120.                 break;
    121.             case 3:
    122.                 color.r  = p;
    123.                 color.g  = q;
    124.                 color.b  = v;
    125.                 break;
    126.             case 4:
    127.                 color.r  = t;
    128.                 color.g  = p;
    129.                 color.b  = v;
    130.                 break;
    131.             default:
    132.                 color.r  = v;
    133.                 color.g  = p;
    134.                 color.b  = q;
    135.                 break;
    136.         }
    137.        
    138.         return color;
    139.     }
    140.    
    141.     /**
    142.     * Format nicely
    143.     */
    144.     function ToString():String
    145.     {
    146.         return String.Format("h: {0:0.00}, s: {1:0.00}, v: {2:0.00}, a: {3:0.00}", h, s, v, a);
    147.     }
    148. }
    149.  
    You can then do:

    Code (csharp):
    1.  
    2. var randomHSV = new ColorHSV(Random.Range(0.0, 1.0), 1.0, 1.0);
    3. var color = randomHSV.ToColor();
    4.  
     
    efge and Xform like this.
  16. housewarmer

    housewarmer

    Joined:
    Apr 18, 2008
    Posts:
    74
    Oooh, nice! That is a very handy tool you've put together!
     
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yeah, cool script! Should be on the wiki.

    --Eric
     
  18. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    Yeah, I know. When things calm down I'm going to have the whole office put up a bunch of our stuff to the wiki. It's just that things never seem to calm down...

    Soon!
     
  19. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I thought it was obvious how awesome and relevant the script was earlier, so I didn't comment on that. But I'm going to join the bandwagon now.

    Awesome script!

    I nearly always use HSV controls to choose colors for artwork, and it makes total sense to do the same here. (Corel Painter's hue circle with saturation/value triangle is my personal favorite.)
     

    Attached Files:

  20. Angrycrow

    Angrycrow

    Joined:
    Jan 29, 2011
    Posts:
    34
    That's a pretty righteous script. So useful!
     
  21. ant123

    ant123

    Guest

    Joined:
    May 31, 2011
    Posts:
    13
    HSV and HSL are the way to go! what about Unity function: EditorGUIUtility.HSVToRGB

    Question: Is there any way of making this a seperate script and calling to it from javascripts?

    I am a Noob and i am learning javascript, the script could be used in any script that calls colors, but can it be used as a seperate converter script, maybe inside a empty gameObject?

    Thanks!

    I just came from a page that said:
    Converting to another palette is a far superior way to do this. There's a reason they do that: other palettes are 'perceptual' - that is, they put similar seeming colors close together, and adjusting one variable changes the color in a predictable manner. None of that is true for RGB, where there's no obvious relationship between colors that "go well together".
     
    Last edited: Jul 22, 2011
  22. invicticide

    invicticide

    Joined:
    Nov 15, 2009
    Posts:
    109
    Matthew, I love you for this. <3
     
  23. Karsnen_2

    Karsnen_2

    Joined:
    Nov 28, 2011
    Posts:
    89
    Wonderful Script :

    I have made an Equivalent C# for those who might need. The last string method pulled in an error which has been commented. I wish to rectify it in the near future. Forgive my lazyness.



    Code (csharp):
    1.  
    2. /**
    3.    
    4.     * A color in HSV space
    5.    
    6.     */
    7.    
    8.     class ColorHSV : System.Object
    9.    
    10.     {
    11.    
    12.         float h = 0.0f;
    13.    
    14.         float s = 0.0f;
    15.    
    16.         float v = 0.0f;
    17.    
    18.         float a = 0.0f;
    19.    
    20.        
    21.    
    22.         /**
    23.    
    24.         * Construct without alpha (which defaults to 1)
    25.    
    26.         */
    27.    
    28.         public ColorHSV ( float h ,   float s ,   float v  )
    29.         {
    30.    
    31.             this.h = h;
    32.    
    33.             this.s = s;
    34.    
    35.             this.v = v;
    36.    
    37.             this.a = 1.0f;
    38.    
    39.         }
    40.    
    41.        
    42.    
    43.         /**
    44.    
    45.         * Construct with alpha
    46.    
    47.         */
    48.    
    49.         public ColorHSV ( float h ,   float s ,   float v ,   float a  )
    50.         {
    51.    
    52.             this.h = h;
    53.    
    54.             this.s = s;
    55.    
    56.             this.v = v;
    57.    
    58.             this.a = a;
    59.    
    60.         }
    61.    
    62.        
    63.    
    64.         /**
    65.    
    66.         * Create from an RGBA color object
    67.    
    68.         */
    69.    
    70.         public ColorHSV ( Color color  )
    71.         {
    72.    
    73.             float min = Mathf.Min(Mathf.Min(color.r, color.g), color.b);
    74.    
    75.             float max = Mathf.Max(Mathf.Max(color.r, color.g), color.b);
    76.    
    77.             float delta = max - min;
    78.    
    79.      
    80.    
    81.             // value is our max color
    82.    
    83.             this.v = max;
    84.    
    85.      
    86.    
    87.             // saturation is percent of max
    88.    
    89.             if(!Mathf.Approximately(max, 0))
    90.    
    91.                 this.s = delta / max;
    92.    
    93.             else
    94.    
    95.             {
    96.    
    97.                 // all colors are zero, no saturation and hue is undefined
    98.    
    99.                 this.s = 0;
    100.    
    101.                 this.h = -1;
    102.    
    103.                 return;
    104.    
    105.             }
    106.    
    107.      
    108.    
    109.             // grayscale image if min and max are the same
    110.    
    111.             if(Mathf.Approximately(min, max))
    112.    
    113.             {
    114.    
    115.                 this.v = max;
    116.    
    117.                 this.s = 0;
    118.    
    119.                 this.h = -1;
    120.    
    121.                 return;
    122.    
    123.             }
    124.    
    125.            
    126.    
    127.             // hue depends which color is max (this creates a rainbow effect)
    128.    
    129.             if( color.r == max )
    130.    
    131.                 this.h = ( color.g - color.b ) / delta;         // between yellow  magenta
    132.    
    133.             else if( color.g == max )
    134.    
    135.                 this.h = 2 + ( color.b - color.r ) / delta; // between cyan  yellow
    136.    
    137.             else
    138.    
    139.                 this.h = 4 + ( color.r - color.g ) / delta; // between magenta  cyan
    140.    
    141.      
    142.    
    143.             // turn hue into 0-360 degrees
    144.    
    145.             this.h *= 60;
    146.    
    147.             if(this.h < 0 )
    148.    
    149.                 this.h += 360;
    150.    
    151.         }
    152.    
    153.        
    154.    
    155.         /**
    156.    
    157.         * Return an RGBA color object
    158.    
    159.         */
    160.    
    161.         public Color ToColor ()
    162.         {
    163.    
    164.             // no saturation, we can return the value across the board (grayscale)
    165.    
    166.             if(this.s == 0 )
    167.    
    168.                 return new Color(this.v, this.v, this.v, this.a);
    169.    
    170.      
    171.    
    172.             // which chunk of the rainbow are we in?
    173.    
    174.             float sector = this.h / 60;
    175.    
    176.            
    177.    
    178.             // split across the decimal (ie 3.87f into 3 and 0.87f)
    179.    
    180.             int i;
    181.             i = (int)Mathf.Floor(sector);
    182.    
    183.             float f = sector - i;
    184.    
    185.            
    186.    
    187.             float v = this.v;
    188.    
    189.             float p = v * ( 1 - s );
    190.    
    191.             float q = v * ( 1 - s * f );
    192.    
    193.             float t = v * ( 1 - s * ( 1 - f ) );
    194.    
    195.            
    196.    
    197.             // build our rgb color
    198.    
    199.             Color color = new Color(0, 0, 0, this.a);
    200.    
    201.            
    202.    
    203.             switch(i)
    204.    
    205.             {
    206.    
    207.                 case 0:
    208.    
    209.                     color.r = v;
    210.    
    211.                     color.g = t;
    212.    
    213.                     color.b = p;
    214.    
    215.                     break;
    216.    
    217.                 case 1:
    218.    
    219.                     color.r = q;
    220.    
    221.                     color.g = v;
    222.    
    223.                     color.b = p;
    224.    
    225.                     break;
    226.    
    227.                 case 2:
    228.    
    229.                     color.r  = p;
    230.    
    231.                     color.g  = v;
    232.    
    233.                     color.b  = t;
    234.    
    235.                     break;
    236.    
    237.                 case 3:
    238.    
    239.                     color.r  = p;
    240.    
    241.                     color.g  = q;
    242.    
    243.                     color.b  = v;
    244.    
    245.                     break;
    246.    
    247.                 case 4:
    248.    
    249.                     color.r  = t;
    250.    
    251.                     color.g  = p;
    252.    
    253.                     color.b  = v;
    254.    
    255.                     break;
    256.    
    257.                 default:
    258.    
    259.                     color.r  = v;
    260.    
    261.                     color.g  = p;
    262.    
    263.                     color.b  = q;
    264.    
    265.                     break;
    266.    
    267.             }
    268.             return color;
    269.    
    270.         }
    271.    
    272.        
    273.    
    274.         /**
    275.    
    276.         * Format nicely
    277.    
    278.        
    279.    
    280.         string ToString ()
    281.         {
    282.    
    283.             return String.Format("h: {0:0.00f}, s: {1:0.00f}, v: {2:0.00f}, a: {3:0.00f}", h, s, v, a);
    284.    
    285.         }
    286.         */
    287.  
    288.  
    289.  


    Cheers!!!
     
    AlanMattano and jawadzeb0000 like this.
  24. MacyK

    MacyK

    Joined:
    May 10, 2013
    Posts:
    15
    Some how the original code only gives me red.

    I Changed some of the code to the following:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. /**
    5. * A color in HSV space
    6. */
    7.  
    8. class ColorHSV : System.Object
    9. {
    10.     private float h;
    11.     private float s;
    12.     private float v;
    13.     private float a;
    14.    
    15.     /**
    16.     * Construct without alpha (which defaults to 1)
    17.     */
    18.  
    19.     public ColorHSV (float h ,float s ,float v){
    20.         this.h = h;
    21.         this.s = s;
    22.         this.v = v;
    23.         this.a = 1.0f;
    24.     }
    25.    
    26.     /**
    27.     * Construct with alpha
    28.     */
    29.  
    30.     public ColorHSV (float h , float s, float v,float a){
    31.         this.h = h;
    32.         this.s = s;
    33.         this.v = v;
    34.         this.a = a;
    35.     }
    36.  
    37.     /**
    38.     * Create from an RGBA color object
    39.     */
    40.  
    41.     public ColorHSV ( Color color  ){
    42.         float min = Mathf.Min(Mathf.Min(color.r, color.g), color.b);
    43.         float max = Mathf.Max(Mathf.Max(color.r, color.g), color.b);
    44.         float delta = max - min;
    45.         // value is our max color
    46.         this.v = max;
    47.         // saturation is percent of max
    48.  
    49.         if(!Mathf.Approximately(max, 0)){
    50.             this.s = delta / max;
    51.         }else{
    52.             // all colors are zero, no saturation and hue is undefined
    53.             this.s = 0;
    54.             this.h = -1;
    55.             return;
    56.         }
    57.  
    58.         // grayscale image if min and max are the same
    59.         if(Mathf.Approximately(min, max)){
    60.             this.v = max;
    61.             this.s = 0;
    62.             this.h = -1;
    63.             return;
    64.         }
    65.         // hue depends which color is max (this creates a rainbow effect)
    66.         if( color.r == max ){
    67.             this.h = ( color.g - color.b ) / delta;         // between yellow  magenta
    68.         }else if( color.g == max ){
    69.             this.h = 2 + ( color.b - color.r ) / delta; // between cyan  yellow
    70.         }else{
    71.             this.h = 4 + ( color.r - color.g ) / delta; // between magenta  cyan
    72.         }
    73.  
    74.         // turn hue into 0-360 degrees
    75.         this.h *= 60;
    76.         if(this.h < 0 ){
    77.             this.h += 360;
    78.         }
    79.     }
    80.  
    81.     /**
    82.     * Return an RGBA color object
    83.     */
    84.  
    85.     public Color ToColor (){
    86.         // no saturation, we can return the value across the board (grayscale)
    87.         if(this.s == 0 ){
    88.             return new Color(this.v, this.v, this.v, this.a);
    89.         }
    90.         // which chunk of the rainbow are we in?
    91.         float sector = this.h / 60;
    92.         // split across the decimal (ie 3.87f into 3 and 0.87f)
    93.         int i;
    94.         i = (int)Mathf.Floor(sector);
    95.         float f = sector - i;
    96.         float v = this.v;
    97.         float p = v * ( 1 - s );
    98.         float q = v * ( 1 - s * f );
    99.         float t = v * ( 1 - s * ( 1 - f ) );
    100.         // build our rgb color
    101.         Color color = new Color(0, 0, 0, this.a);
    102.         switch(i){
    103.             case 0:
    104.                 color.r = v;
    105.                 color.g = t;
    106.                 color.b = p;
    107.                 break;
    108.             case 1:
    109.                 color.r = q;
    110.                 color.g = v;
    111.                 color.b = p;
    112.                 break;
    113.             case 2:
    114.                 color.r  = p;
    115.                 color.g  = v;
    116.                 color.b  = t;
    117.                 break;
    118.             case 3:
    119.                 color.r  = p;
    120.                 color.g  = q;
    121.                 color.b  = v;
    122.                 break;
    123.             case 4:
    124.                 color.r  = t;
    125.                 color.g  = p;
    126.                 color.b  = v;
    127.                 break;
    128.             default:
    129.                 color.r  = v;
    130.                 color.g  = p;
    131.                 color.b  = q;
    132.                 break;
    133.         }
    134.         return color;
    135.     }
    136.  
    137.     public static Color GetRandomColor(float h ,float s ,float v){
    138.         ColorHSV col = new ColorHSV(h,s,v);
    139.         return col.ToColor();
    140.     }
    141. }
    142.  
    143.    
    144.  
    You can get a random bright color by calling this:

    Code (csharp):
    1.  
    2. Color newColor = ColorHSV.GetRandomColor(Random.Range(0.0f, 360f, 1, 1);
    3.  
    You Have to set the H value random from 0 to 360 to get more than just red.
     
  25. ArtOfWarfare

    ArtOfWarfare

    Joined:
    Sep 28, 2013
    Posts:
    23
    Everyone, knock it off with that monster script. It may have been the best answer when it was first shared 5 years ago, but ant123 posted a hint to a one line function that generates a random aesthetically pleasing color 2 years ago. Here's the one liner:

    Code (csharp):
    1. EditorGUIUtility.HSVToRGB(Random.Range(0.0, 1.0), 1, 1);
     
  26. invicticide

    invicticide

    Joined:
    Nov 15, 2009
    Posts:
    109
    That's certainly about a million percent more elegant... but what if you need to generate the color at runtime? You won't have EditorGUIUtility available in that case, yeah?
     
  27. ArtOfWarfare

    ArtOfWarfare

    Joined:
    Sep 28, 2013
    Posts:
    23
    I have to confess my inexperience with Unity right now - I don't understand what you're saying. When I hit the run button in Unity, this works for me. I have no idea if it's going to behave differently when I try running as a stand alone or on a different platform.
     
  28. invicticide

    invicticide

    Joined:
    Nov 15, 2009
    Posts:
    109
    Yeah, inside the editor you'll be fine, but in a standalone build you'll be hosed. :(

    The reason is that the EditorGUIUtility class is defined in the UnityEditor assembly, which you need to import by placing "using UnityEditor" at the top of your script. The UnityEditor assembly is only loaded when you're running inside the editor; it's not loaded for standalone builds. When you try to make a standalone build that contains UnityEditor-dependent scripts, you'll get this error:

    So how do you ever make a standalone build when you've created editor scripts? Well, scripts that depend on UnityEditor are supposed to live inside a subfolder called "Editor" in your project. When you make a standalone build, everything under an "Editor" folder is ignored (the intent is that these would be Unity editor extensions and tools and stuff which you don't need/want to ship with your game).

    The Catch-22 is that stuff inside an Editor folder can't be used for gameplay/runtime.
     
    Last edited: Oct 1, 2013
  29. ArtOfWarfare

    ArtOfWarfare

    Joined:
    Sep 28, 2013
    Posts:
    23
    So there was a developer who went through the effort of programming this functionality but honestly couldn't imagine it being useful in a game? I have no good words for him.

    After looking over the function for converting HSV to RGB, I've noticed that all one has to do to get a color with maximum Saturation and Value with a random Hue in RGB is:

    1 - Assign a random float between 0.0 and 1.0 to a random RGB channel.
    2 - Assign 1.0 and 0.0 to the other two remaining RGB channels.

    I'm sure there's a very elegant way of way of writing this in code, but I'm not quite sure what it is.
     
    Last edited: Oct 6, 2013
  30. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Here's how to generate colors with random hue full saturation, as describes in the post above, it's not especially elegant, though it is pretty smart

    Code (csharp):
    1. Color myColor;
    2. bool mode = Random.value > 0.5f;
    3. float rand = Random.value;
    4. int index = Random.Range( 0, 3 );
    5. myColor[index] = rand;
    6. myColor[( index + ( mode ? 1 : 2 ) ) % 3] = 1;
    7. myColor[( index + ( mode ? 2 : 1 ) ) % 3] = 0;
    8. renderer.material.color = myColor;
    also, you can control the HSV value by changing the 1 on line 6 to Value, and multiplying rand on line 5 by Value



    Link: https://dl.dropboxusercontent.com/u/157182894/Builds/test/Colors/Colors.html
     
  31. pdunton

    pdunton

    Joined:
    Jul 16, 2013
    Posts:
    2
    Can you please turn this into javascript?! I love it!
     
  32. jawadzeb0000

    jawadzeb0000

    Joined:
    Nov 16, 2015
    Posts:
    3
    thank you @Karsnen_2

    the ToStiring method is not working because of 'String' with capital 'S' and without
    @override Annotation it is working when i wrote with small 's' and @override. here is the code:
    Code (csharp):
    1.  
    2.  
    3. public override string ToString()        {
    4.             return string.Format ("h: {0:0.00}, s: {1:0.00}, v: {2:0.00}, a: {3:0.00}", h, s, v, a);
    5.         }
    6.  
    7.  
    8.  
     
    Last edited: Jan 17, 2016
  33. invicticide

    invicticide

    Joined:
    Nov 15, 2009
    Posts:
    109
    That's probably because the String class is defined in the System namespace. While you can use lowercase "string" as a syntactic shortcut, the real solution would be one of the following:

    Code (CSharp):
    1. using System; // Allows using String directly
    2.  
    3. public class ColorHSV
    4. {
    5.     public string ToString()
    6.     {
    7.         // We can do this now because we've imported the System namespace at the top of this file
    8.         return String.Format(...);
    9.     }
    10. }
    or...

    Code (CSharp):
    1. public class ColorHSV
    2. {
    3.     public string ToString()
    4.     {
    5.         // Explicitly reference the System namespace, since we have not imported it at the top of this file
    6.         return System.String.Format(...);
    7.     }
    8. }
     
  34. yogeshkainthola

    yogeshkainthola

    Joined:
    Jul 12, 2016
    Posts:
    1

    How to use this script in unity i am a newbie in unity ...just started 2 weeks ago...
     
  35. testure

    testure

    Joined:
    Jul 3, 2011
    Posts:
    81
    This thread is hella old, but seeing as people are still finding it via search, there's actually a simpler way in modern times (considering that Unity finally added HSV support to the Color class). What Jessy wrote way back when this thread was first started is still true- if you just make colors by picking RGB values at random there's a 99% chance the color will be terrible looking. Most of the time, when people want a random color, what they actually want is a random hue, which used to require writing a bunch of RGB->HSV code, but now it's built into Unity.

    Code (CSharp):
    1. public static Color RandomColor(float s, float v)
    2.     {
    3.         var hue = Random.Range(0f, 1f);
    4.         return Color.HSVToRGB( hue, s, v);
    5.     }
    this code will yield pleasing and consistent random colors.
     
    Last edited: Aug 20, 2016
    Hogfather, Besbes_Salma, efge and 3 others like this.
  36. alicederyn

    alicederyn

    Joined:
    Nov 28, 2020
    Posts:
    1
    Not sure when it got added, but you can now do
    Random.ColorHSV(minHue, maxHue, s, s, v, v)
     
    FileThirteen likes this.