Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

C# Color make it lighter?

Discussion in 'Scripting' started by AmRafay1, Jun 14, 2018.

  1. AmRafay1

    AmRafay1

    Joined:
    Oct 22, 2013
    Posts:
    632
    Hi there,

    How can i make a C# Color lighter or darker?
    also having the ability to check if a color is darker or lighter.
    I tried all the codes online but they never seem to work.

    Please advise
     
    Last edited: Jun 14, 2018
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    660
    Depending on what you specifically want to do with the colors, one simple approach is just to multiply the color by a value higher than 1 (lighter) or lower than one (darker). For example:

    Code (CSharp):
    1.             var red = Color.red;
    2.             var lightRed = red * 1.5f;
    3.             var darkRed = red * 0.5f;
    4.  
    5.             // Correct alpha.
    6.             lightRed.a = 1;
    7.             darkRed.a = 1;
    Note that this affects the alpha channel, which is why I've reset it to 1. Alternately you can create a new color with:

    Code (CSharp):
    1. var lightRed = new Color(red.r * 1.5f, red.g * 1.5f, red.b * 1.5f);
     
    AmRafay1 likes this.
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,205
    And checking darker/lighter you'd usually do by turning them into a greyscale value and then compare them.
     
    AmRafay1 likes this.
  4. AmRafay1

    AmRafay1

    Joined:
    Oct 22, 2013
    Posts:
    632
    Thanks, how can i detect if a color is darker (so i light only which is darker by some percentage)
     
  5. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    287
    Color.grayscale

    then if the grayscale value is lower than what you consider dark (will always be between 0 and 1), you lighten it.
     
    AmRafay1 likes this.
  6. AmRafay1

    AmRafay1

    Joined:
    Oct 22, 2013
    Posts:
    632
    Thanks guys :)
     
  7. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    712
    whileBreak and AmRafay1 like this.