- Home /
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
umm .. WHAT IS IT you're trying to change?
a 2D square of color? A dinosaur? what ?
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.
A 2d square of color indeed. I thought I gave enough info to convey that, but yes.
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) );
nice GIF =]
Did you look at changing the slider values from between 0-255 to 0.0-1.0 ?
Answer by Semicolon · Aug 19, 2012 at 05:13 PM
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.
Your answer
Follow this Question
Related Questions
Is changing the color of GUIText unavailable in the Free Edition? 1 Answer
Dynamically changing part of a texture v2 1 Answer
On Touch change color 2 Answers
Material doesn't have a color property '_Color' 4 Answers
Change the color of a child 1 Answer