Search Unity

C# Color make it lighter?

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

  1. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,945
    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:
    4,196
    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);
     
    Kokowolo, vabster and jGate99 like this.
  3. jvo3dc

    jvo3dc

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

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,945
    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:
    289
    Color.grayscale

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

    jGate99

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

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
  8. ZackOfAllTrades

    ZackOfAllTrades

    Joined:
    Oct 30, 2016
    Posts:
    3
    Color.Lerp(originalColor, Color.black, .5f); To darken by 50%
    Color.Lerp(originalColor, Color.white, .5f); To lighten by 50%
     
    karalabe1, bbyford, FantoLV and 10 others like this.