Colors for DrawSolidRectangleWithOutline broken?

I have the following Editor window code:

using UnityEngine;
using UnityEditor;
public class TestEditor : EditorWindow
{
    [MenuItem("Window/TestEditor")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(TestEditor));
    }
    void OnGUI()
    {
        for (int i = 0; i < 2; i++)
        {
            Handles.DrawSolidRectangleWithOutline(new Rect(10, 10 + (30 * i), 100, 5), Color.green, Color.blue);
            Handles.color = Color.red;
        }
    }
}

The results are … not what I expect. The first rectangle draws correctly, the second one is black.
6817568--791840--TestEditor1.png

If I comment out the Handles.color = Color.Red; portion, everything draws correctly.

6817568--791843--TestEditor2.png

It seems that any change to the Handles.color causes all subsequent calls to DrawSolidRectangleWithOutline to draw black.

These (like most) colors are multiplied. Red (1,0,0) times Green (0,1,0) is black.

Same with red (1,0,0) times blue (0,0,1).

If you always draw white you can control the colors effectively with the Handles.color property.

EDIT: looking at what you’re doing above, I suggest you simply decide the color you want from a table already, pass it into the box call directly, no need for stateful Handle.color calls.

2 Likes

I noticed this shortly after posting this topic. Is that a bug, or is it the intended behavior? It does not mention this behavior in the documentation for DrawSolidRectangleWithOutline.

The above is just an example. I draw many objects, most of which require the Handle.color property.

For now I can just set the color to white, as you have noted, but it would be nice if either the documentation was clarified or this was fixed to not require resetting the color.

The Handles.color property is intended to modify the colors of other things painted through Handles, not replace them. Like @Kurt-Dekker said, this modification is a pointwise multiplication: Red (1,0,0) x Green (0,1,0) = (10,01,0*0) = Black (0,0,0). This is intended behavior.

Were you expecting DrawSolidRectangleWithOutline not to be affected by Handles.color? Rule of thumb for editor windows is that everything is filtered through the current color.

Almost every use of color in shader-ish logic is multiplied color: sprites, materials, UI.Images GUI, GUISkins, Lights, projectors, etc.

It’s also kinda how the real world works. Try shining red light on green plants sometime.

I think they take it for granted.

I understand the concept, and why its not considered a bug, and it makes sense when taken in that context, but adding a little bit to the Handles.color documentation that says its a multiplier, would have saved me hours of debugging and headache; and might just help the next person. At the very least, this topic might show up if they Google the problem.

2 Likes