Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. 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:
    628
    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:
    495
    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,140
    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:
    628
    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:
    272
    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:
    628
    Thanks guys :)
     
  7. tonemcbride

    tonemcbride

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