How do i get a colour for code?

I don’t understand. Who wrote this thing? Why did this colour picker seem like a good idea?

All of unity’s functions take normalised colours, (values in the range 0…1) but this has them in 0-255 format, and there’s no copy to clipboard function.

How do you work like this? am i supposed to get out a calculator three times for every single colour?

I;'m trying to set colour values on things visually to see how they look, and then put them in code for later runtime switching. This is a nightmare to work with. I’ve got thirty different colours to manually choose and write in, and they are very likely to change in future, too.

whyyyy

please tell me theres a better way. :frowning:

For code, a color would simply be made from a vector4. Where the X Y Z represent your Red Blue and Green Channels while your W represents your Alpha channel.

That being said, your best bet would be to write a function that normalizes your color for you.

I’m just writing something up quickly but the code would do this.

-take in a vector4 in standard 0-255 format
-take that vector4 divide each component by 255
-spit back out a normalized vector4 color.

That way you could simply write out your colors in standard 8-bit format, and then have your function, named something like

Vector4 ColorNormalizer(Vector4 ColorParameter )

just take in the vector4 and spitout a new one for you to use that is simple for you to use.

1 Like

Now its hard to understand particularly what part you’re having trouble with, but an alternative would be to feed in a public Color[ ] array, which you could change and tweak using a color picker through the inspector, in that script’s inspector properties, and then just call on which ever array index has the color you are wanting to use for what ever you are wanting to use it for.

1 Like

Just divide by 255 to convert.

Yup, it’s kinda dumb. It’s probably made like that to look similar to what a color picker looks like in drawing software (it’s almost identical to the one in Paint.net), but I feel your woes.

If you really feel like you need a conversion, you could make a custom editor that draws the 0-1 numbers out next to the color picker. You could even make a wrapper class for Colors that has a drawer that shows the correct values, so you can throw that in where you want it.

I’ve actually used something similar in past projects, so anyway I’ll just post this here if anyone is still having issues with the idea of what i’m talking about…

using UnityEngine;
using System.Collections;

//feed in a vector4 (RGBA color channels)
public Vector4 ColorNormalizer(Vector4 testcolor){
Vector4 normalized;

normalized = ((testcolor.x/255),(testcolor.y/255),(testcolor.z/255),(testcolor.w/255));
return normalized;
//returns normalized vector4.
}

Or option B.

step 1.:

using UnityEngine;
using System.Collections;

public Color[ ] SetOfColors;

step2: Go to inspector and set a size for the array, and set color values for each array element.

step3: Simply call the color as SetOfColors[key];

CastleIsGreat: Thx for you advice, i am the one who also facing this problem. There are no console Error. However, the function do not shown in UGUI. Can anyone help me? Thx

    public void ChgColor(Vector4 testcolor)
    {
        testcolor = new Vector4((testcolor.x/255), (testcolor.y/255), (testcolor.z/255),(testcolor.w/255));
    }

Just use a Color32

Or alternatively simply use a serialised colour via the inspector.

2 Likes

What’s with all these Vector4 suggestions. Unity has Color32 for 255 etc type values and Color for 0-1. And if you want to convert its simply

Color32 c = (Color32)MyColor

A vector4 is parameter for the color function, but yes your idea would also work well.

Are you always this dramatic? Especially coming from the person who refused to use LayerMasks, opting instead to hardcode the bit-shifted integer into his code (which required a calculator) :slight_smile:

It’s also worth noting that you won’t mind the 0-255 range the first time you have to transfer a color from Photoshop to Unity.

I agree with @Kiwasi : don’t put color literals or constants into your code, especially if you think you might ever want to tweak them. Instead, expose them somewhere as public properties, and tweak them in the inspector.

You could, for example, make a Palette class with a bunch of named colors, which you throw onto a scene object and set all the colors on. Give it an “instance” accessor to easily find it from code, and then wherever you need a color in your code, you just use Palette.instance.sky, Palette.instance.goblinSkin, etc.

The only time I use color constants in my code are when they are special colors which require a specific value, like pure black because my shader uses that to mean transparent, or something like that.

2 Likes

The “code” in question is a dictionary class for storing data.