Daniel Lip
Daniel Lip

Reputation: 11319

Why in unity editor window script when making new Color instance it's not changing to the color?

void DrawItemBackgroundColor(Rect bgRect)
        {
            if (Event.current.type == EventType.Repaint)
            {
                Color oldColor = GUI.color;
                GUI.color = new Color(255, 182, 193);

                var rect = bgRect;
                rect.height = Styles.headerBackground.fixedHeight;
                Styles.headerBackground.Draw(rect, false, false, false, false);

                rect.y += rect.height;
                rect.height = bgRect.height - rect.height;
                Styles.background.Draw(rect, false, false, false, false);

                GUI.color = oldColor;
            }
        }

This should change the color to pink :

GUI.color = new Color(255, 182, 193);

But it does nothing.

But if I'm doing :

GUI.color = Color.red; 

It will change it to red the problem is that Color don't have all the colors only some.

Upvotes: 1

Views: 411

Answers (2)

Daniel Lip
Daniel Lip

Reputation: 11319

Solution is to use Color32 :

void DrawItemBackgroundColor(Rect bgRect)
{
    if (Event.current.type == EventType.Repaint)
    {
        Color oldColor = GUI.color; 
        GUI.color = new Color32(255, 182, 193,100);

        var rect = bgRect;
        rect.height = Styles.headerBackground.fixedHeight;
        Styles.headerBackground.Draw(rect, false, false, false, false);

        rect.y += rect.height;
        rect.height = bgRect.height - rect.height;
        Styles.background.Draw(rect, false, false, false, false);

        GUI.color = oldColor;
    }
}

This line make it color in nice light pink color :

GUI.color = new Color32(255, 182, 193,100);

Upvotes: 0

TimChang
TimChang

Reputation: 2417

For your case :

GUI.color = new Color(255f/255f, 182f/255f, 193f/255f);
  1. Color RGB value range is 0~1
  2. Color32 RGB value range is 0~255

So you can use Color32 by 0~255 or Use Color by 0~1;

Upvotes: 1

Related Questions