x


Creating a photoshop-esque slider for picking colors

Hey there!

I've tried my hands at creating something like this. If you haven't used photoshop and don't understand what's happening from looking at the pictures I'll try to explain it:

There are 3 sliders. Red, green and blue, all of them have a gradient applied to them from Color(0, 0, 0) to COlor(red, green, blue). They also each control the value of their color, so I want the slider to function as a slider would and simply change the background image in real-time.

Here's what I tried (with horrible results)

Texture2D UpdateSliderTexture(Texture2D texture)
    {
        Texture2D returnTexture = texture;
        for (int y = 0; y < 8; y++)
        {
            for (int x = 0; x < 255; x++)
            {
                returnTexture.SetPixel(x, y, new Color(colorR * x, colorG * x, colorB * x));
            }
        }
        returnTexture.Apply();

        return returnTexture;
    }

Horrible as in it freezes. The documentation told me that Apply() is expensive but I don't know how else I'd go about doing this.

Any ideas? Here's the slider code in case you'd like to know:

colorR = GUI.HorizontalSlider(new Rect(16, 0, 255, 16), colorR, 0, 255, colorSliderStyle, colorSliderThumb);

I made a gif just to clarify: http://i.imgur.com/LshWC.gif

Edit: I also fixed the broken code... Sorry for the confusion

more ▼

asked Aug 19 '12 at 02:48 PM

Semicolon gravatar image

Semicolon
31 1 2

umm .. WHAT IS IT you're trying to change?

a 2D square of color? A dinosaur? what ?

Aug 19 '12 at 03:44 PM Fattie

FTR this sort of thing is trivial with the beloved 2DToolkit, you just do this:

yourSprite.color.r = 0.7;

so that's one easy solution.

Aug 19 '12 at 03:46 PM Fattie

A 2d square of color indeed. I thought I gave enough info to convey that, but yes.

Aug 19 '12 at 03:59 PM Semicolon

Color needs values between 0 and 1, try changing your slider values

colorR = GUI.HorizontalSlider(new Rect(16, 0, 255, 16), colorR, 0.0, 1.0, colorSliderStyle, colorSliderThumb);

http://docs.unity3d.com/Documentation/ScriptReference/Color.html

OR, you could divide the value by 255 in SetPixel

returnTexture.SetPixel( x, y, new Color(colorR / 255, colorG / 255, colorB / 255) );
Aug 19 '12 at 04:18 PM alucardj

nice GIF =]

Did you look at changing the slider values from between 0-255 to 0.0-1.0 ?

Aug 19 '12 at 04:55 PM alucardj
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Christ now I feel stupid... I changed it to go between 0 and 1 instead. I'm used to XNA and they used 0-255. The crashing also came from me using the wrong format. I changed it to ARGB 32 bit and it worked.

more ▼

answered Aug 19 '12 at 05:13 PM

Semicolon gravatar image

Semicolon
31 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x508
x380
x29
x23
x3

asked: Aug 19 '12 at 02:48 PM

Seen: 301 times

Last Updated: Aug 19 '12 at 05:13 PM