Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

determining colour shade from rgb values.

1 view
Skip to first unread message

kem

unread,
Jan 2, 2003, 6:52:45 PM1/2/03
to
Hi,
I need to be able to determine if the color of a pixel is a certain
shade of a color. i.e. i might have the rgb values of a pixel - from this i
need to determine if the pixel is a shade of a certain colour i.e. red or
blue.

If anyone knows any ways of doing this using RGB ratios or
whatever I'd be very greatful. I need to be able to determine Yellow,
Green, Blue, Black, Red, and if possible light blue from dark blue :) I'll
be implementing the algorithm in java.

I have no experience with graphics work of this kind and have searched
newsgroup archives and search engines but cant find anything useful! Any
suggestions would be very greatfully received!

Thanks

Kemal Enver
u0...@dcs.shef.c.uk


Hans-Bernhard Broeker

unread,
Jan 3, 2003, 5:26:43 AM1/3/03
to
kem <kemal...@f2s.com> wrote:

> I need to be able to determine if the color of a pixel is a
> certain shade of a color. i.e. i might have the rgb values of a
> pixel - from this i need to determine if the pixel is a shade of a
> certain colour i.e. red or blue.

Sounds like a transformation to some other color coordinate system is
what you need. Try HSV or CIE Lab. See the Colour FAQ (posted here
regularly) for more details.
--
Hans-Bernhard Broeker (bro...@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Roger Willcocks

unread,
Jan 3, 2003, 6:56:59 AM1/3/03
to

"kem" <kemal...@f2s.com> wrote in message
news:av2jcc$110$1...@venus.btinternet.com...

> Hi,
> I need to be able to determine if the color of a pixel is a certain
> shade of a color. i.e. i might have the rgb values of a pixel - from this
i
> need to determine if the pixel is a shade of a certain colour i.e. red or
> blue.
>
> If anyone knows any ways of doing this using RGB ratios or
> whatever I'd be very greatful. I need to be able to determine Yellow,
> Green, Blue, Black, Red, and if possible light blue from dark blue :)
I'll
> be implementing the algorithm in java.
>

You should convert to HSV (hue, saturation, value) - the H part tells you
the basic colour, the S part the 'strength' of the colour, i.e. whether it's
a pastel shade or a pure colour, and the value part tells you how bright it
is.

> I have no experience with graphics work of this kind and have searched
> newsgroup archives and search engines but cant find anything useful! Any
> suggestions would be very greatfully received!
>

See e.g. http://www.cs.rit.edu/~ncs/color/t_convert.html

> Thanks
>
> Kemal Enver
> u0...@dcs.shef.c.uk
>

--
Roger
http://www.rops.org

Brian Stone

unread,
Jan 3, 2003, 1:54:43 PM1/3/03
to
"kem" <kemal...@f2s.com> wrote in message news:<av2jcc$110$1...@venus.btinternet.com>...

You'll want to convert RGB into HSV.... Hue Saturation and Value. HSV
is also referred to as HSB (hue saturation and brightness). However,
we usually use HSV to refer to this color model so that the B's aren't
confused between Blue and Brightness. Here is how to convert RGB to
HSV...

// RGB to HSV:
max = Maximum(R, G, B);
min = Minimum(R, G, B);

V = max;

if (V != 0)
S = (V - min) / V;
else
S = 0;

if (S != 0)
{
if (V == R)
H = (G - B) / (max - min);
else if (V == G)
H = 2 + (B - R) / (max - min);
else if (V == B)
H = 4 + (R - G) / (max - min);

H *= 60; // Converts Hue to degrees.
if (H < 0)
H += 360; // Make Hue positive.
}
else
H = 0; // The hue is actually undefined if saturation and
value is zero.

Hue ranges from 0 to 359 degrees. Saturation ranges from 0 to 1. The
range of Value is the same as the range of the RGB values you started
with.

The easiest way to figure out what colors refer to which values of Hue
is to look at a color wheel. But, I can tell you that 0 degrees is
Red, 120 degrees is Green, and 240 degrees is Blue.

If you wanted to determine if a particular pixel is "Blue", for
instance, you could see if the Hue is between 180 and 270 degrees.
Note:180 degrees is Cyan.

This code was derived from that found in "Computer Graphics:
Principles and Practice, Second Edition in C."

-- Brian Stone

kem

unread,
Jan 3, 2003, 7:47:28 PM1/3/03
to
Hi,
Thanks for all that advise guys. I've now got a system that can work
out the colour shade, as long as the saturation is at 100%. As I'm the only
one who will be using this prog I might just leave it as it is, but I would
be interested to know how I could take into consideration the saturation.
Any ideas are welcomed :)

Kemal Enver
u0...@dcs.shef.ac.uk

Myles

unread,
Jan 3, 2003, 8:05:45 PM1/3/03
to

"kem" <kemal...@f2s.com> wrote in message
news:av5av0$bdp$1...@knossos.btinternet.com...

You can set a threshold for saturation, below which you cannot identify the
hue. For me, on my monitor, this is S=20 out of 240. Then colors below this
threshold are black, gray, or white depending on the value of the remaining
component.

M.

0 new messages