Get value of R, G, B by color

I have user type the color (e.g. White, Red,...) so how can I get value of R, G, B with the color of user gave?

I have a function (float r, float g, float b) to set color a node. So i let the user type the color name they want, then i want to convert that color name to get r, g, b value for my function.

preguntado el 31 de julio de 12 a las 09:07

I have a function (float r, float g, float b) to set color a node. So i let the user type the color name they want, then i want to convert that color name to get r, g, b value for my function. -

"So i let the user type the color name they want" Monasterio, anyone? Offer the user a JColorChooser en lugar de. -

3 Respuestas

Color defines a limitado number of enums representing basic colours. This code excerpt uses reflection to map from their names to the RGB representations.

Other than that you'll have to maintain a map of colors/RGB values, or possibly a set of system properties, and use Color.getColor(String name).

Respondido 31 Jul 12, 09:07

I have a function (float r, float g, float b) to set color a node. So i let the user type the color name they want, then i want to convert that color name to get r, g, b value for my function. - user1565134

Create a Map which contains all your optional colors as Strings together with their RGB representation (For the record, Color is found in the java.awt package):

Map<String,Color> colorMap = new HashMap<String,Color>();
        colorMap.put("white", new Color(255,255,255));
        colorMap.put("red", new Color(255,0,0));

Then use the text entered by the user to lookup your Color in the map:

String userColor = <whereever you get your string from>;
Color result = colorMap.get(userColor.toLowerCase());

Finally, use this Color object which has int values for RGB to retrieve the float values you need and pass them to your function:

yourFunction(Float.valueOf(result.getRed(),Float.valueOf(result.getGreen()),Float.valueOf(result.getBlue()));

If possible I would advise to use integers instead of floats for your RGB, as it will range from 0 to 255 in whole numbers in most if not all cases.

A good place to get all the colors you'd ever need with their RGB's is this website: http://cloford.com/resources/colours/500col.htm

Respondido 31 Jul 12, 11:07

The representation of a color channel as float, while maybe not inutitively so, offers 24 bits of precision (por canal). Just because the decimal representation looks inaccurate to you it doesn't mean it produces inaccuracy when the computer uses it. In fact the conversion int <-> float for 8 bit channel values is perfectly reversible. - Durandal

I agree that it is more accurate, but do you have a logical name for every possible decimal value of a color? Because of the limited number of logical color names people actually use, I'd use integers in this case. Smaller, easier to read and produces a much more readable code as you can leave out all the parsing of Java's awt.Color int values to the respective float values. - Massis87

If you have the color as a String you lookup this Color in a map and this will give you the Red, Green and Blue values.

Respondido 31 Jul 12, 09:07

I have a function (float r, float g, float b) to set color a node. So i let the user type the color name they want, then i want to convert that color name to get r, g, b value for my function. - user1565134

You need to set the colour as well. You can store this information in a Map<String, Color> so you can lookup these values later. btw, normally each red, green, blue is a value between 0 and 255. - pedro laurey

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.