Alok
Alok

Reputation: 949

How to get R-G-B value from color name programmatically

By knowing the color name how can we programmatically find its red-green-blue values in Java?

Upvotes: 2

Views: 10304

Answers (5)

Lii
Lii

Reputation: 12112

If you are writing code for the Eclipse platform then the ColorUtil#getColorValue method is an alternative.

It have access to all the colours for the constants that are defined in the SWT class, also the system colours.

The method is in the org.eclipse.ui.workbench plug-in.

Upvotes: 0

A.Shivaranjani
A.Shivaranjani

Reputation: 51

The javax.swing.text.html.StyleSheet can be used for this:

import javax.swing.text.html.StyleSheet;

StyleSheet s = new StyleSheet();
String rgb = "black";
Color c1 = s.stringToColor(rgb);
r1 = c1.getRed();
g1 = c1.getGreen();
b1 = c1.getBlue();

System.out.println(r1 + ", " + g1 + ", " + b1);

Upvotes: 5

NEKIBUR RAHMAN
NEKIBUR RAHMAN

Reputation: 212

Color c= Color.red;
int rgb=c.getRed()*65536+c.getGreen()*256+c.getBlue();

Is this what you wanted to know?

Upvotes: -1

Stephen C
Stephen C

Reputation: 718826

Since you are using SWT, you may be able to use the ColorRegistry API. There are a couple of ways to get hold of prepopulated registries (JFaceResources.getColorRegistry() and ITheme.getColorRegistry()) though it is not obvious from the javadocs what colors they are prepopulated from, and where the color definitions come from.

Alternatively use create a map and populate it with names based on the SWT.COLOR_XXX constants ans color values obtained by using Display.getSystemColor(...)

Upvotes: 3

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

You can get rgb value from hex value.

following code :

Color aColor = new Color(0xFF0096); // Use the hex number syntax
        aColor.getRGB()

Upvotes: -2

Related Questions