Search Unity

Question Help with math logic for applying different lightness weights on colors

Discussion in 'Scripting' started by flasker, Mar 16, 2023.

  1. flasker

    flasker

    Joined:
    Aug 5, 2022
    Posts:
    193
    I have "colorA" with lightness = 0.69 (0-1)
    I want to decrease its lightness to - 0.32

    thats a -0.37 decrease

    however I also have "colorB" with lightness = 0.16
    and for that color I only want to decrease it to - 0.10

    thats a -0.6 decrease

    How do I achieve this with the same formula for both?

    At the moment I have:

    Code (CSharp):
    1.  
    2. extraLightness= -0.357f;
    3. finalLightness = originalLightness + extraLightness*(originalLightness *1.5f) ;
    4.  
    this code causes "ColorA" to go from 0.69 to the desired 0.32
    however "ColorB" goes from 0.16 to 0.074 (off by ~0.3)

    how do I tune this formula so that colors with less lightness are less affected by "extralightness"? I feel like I am somewhat close to what I want already...

    What can I do for this formula to apply less 0.3 on the darker color? I realize I'm not off by much but I also don't quite understand what I can change to affect the range by which lightness changes.

    I'm multiplying the decrease in lightness by the original lightness so colors with less original lightness get less affected by the change. But how can I modify the range/degree to how much the difference is supposed to be?

    Some pointers with this logic would be appreciated, thanks!
     
  2. Deleted User

    Deleted User

    Guest

    use
    AnimationCurve
    in a range of [0,1] to define the specific function you want. You can set key values in the curve, and then set how you want the curve to interpolate between the two values. In the editor, it shows a graph view of the curve, so its easy to make adjustments there.

    https://docs.unity3d.com/Manual/animeditor-AnimationCurves.html

    You don't need to use it with animations, it will work for anything.
     
  3. flasker

    flasker

    Joined:
    Aug 5, 2022
    Posts:
    193
    I need an actual math formula for this situation
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Look up "How to find a polynomial from given points". If you have only two points, you're probably only going to be able to find a straight line though.
     
  5. flasker

    flasker

    Joined:
    Aug 5, 2022
    Posts:
    193
    thanks I will look into it, I can add more points easily
     
  6. Deleted User

    Deleted User

    Guest

    If you want to interpolate with known tangents and slopes between points, you want to use splines, and probably the cubic hermite spline (which is what the
    AnimationCurve
    uses under the hood).

    Check out https://www.cubic.org/docs/hermite.htm