Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Mixing Colors

Discussion in 'Scripting' started by delamis, Dec 13, 2019.

  1. delamis

    delamis

    Joined:
    Sep 27, 2019
    Posts:
    8
    Hi there, i want to learn mixing colors. i created 3 color, 2 of em for mixing 1 of them for result.

    So now i want to give 2 button and mix that colors for reach that color. i can mix colors but i cant reach that target color. its kinda like mix colors game, anyone know any solution?


    Code (CSharp):
    1. public void ColorRandomizer()
    2.     {
    3.         rightButtonColor = new Color32( (byte)Random.Range(0, 256),
    4.                                         (byte)Random.Range(0, 256),
    5.                                         (byte)Random.Range(0, 256), 255);
    6.  
    7.         rightButton.GetComponent<Image>().color = rightButtonColor;
    8.  
    9.         leftButtonColor = new Color32(  (byte)Random.Range(0, 256),
    10.                                         (byte)Random.Range(0, 256),
    11.                                         (byte)Random.Range(0, 256), 255);
    12.  
    13.         leftButton.GetComponent<Image>().color = leftButtonColor;
    14.  
    15.         targetColor = new Color32((byte)((rightButtonColor.r + leftButtonColor.r) / 2),
    16.                                          (byte)((rightButtonColor.g + leftButtonColor.g) / 2),
    17.                                          (byte)((rightButtonColor.b + leftButtonColor.b)), 255);
    18.  
    19.         targetColorImage.GetComponent<Image>().color = targetColor;
    20.  
    21.         changeableColor = new Color32(0, 0, 0, 255);
    22.  
    23.         ChangeAbleImage.GetComponent<Image>().color = changeableColor;
    24.     }

    Code (CSharp):
    1. public void RightColorButton()
    2.     {
    3.  
    4. changeableColor = new Color32((byte)(rightButtonColor.r / 10 ) ,(byte)(rightButtonColor.g / 10),(byte)(rightButtonColor.b/10), 0);
    5.  
    6.  
    7.         ChangeAbleImage.GetComponent<Image>().color += changeableColor;
    8.     }
     
  2. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Why are you using new Color32 (byte) instead of using new Color?

    I use
    Code (CSharp):
    1.  item.color = new Color(1f, 0.75f, 0.5f);
    where the values within the parentheses are in RGB 0-1.0. RGB 0-255 values don't work in code.

    Sans-titre-1.jpg
     
    Last edited: Dec 13, 2019
  3. delamis

    delamis

    Joined:
    Sep 27, 2019
    Posts:
    8
    hi there, its working too and idk first time working with color. i can try your advice too. but the problem i cant have that random color with mixing 2 color
     
  4. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Sorry, I forgot about that. You can use ColorLerp to mix your two colours.
    Code (CSharp):
    1. item.color = new ColorLerp(colour1, colour2, 0.5f);
     
  5. delamis

    delamis

    Joined:
    Sep 27, 2019
    Posts:
    8
    that lerp can use with buttons ? i want same change with + / - buttons not automatic
     
  6. delamis

    delamis

    Joined:
    Sep 27, 2019
    Posts:
    8
    Like this game
     
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    Sure. Just write a function that uses the code that APSchmidt suggested and assign that funciton to be the OnClick even handler for that button.

    https://docs.unity3d.com/Manual/script-Button.html