Funny, was just messing with this now. Here's a striped down version.

Find RGB first:

  c = 0xc0ffee;
  r = c >> 16 & 0xff;
  g = c >> 8 & 0xff;
  b = 0xff;

Brightness/Value according to HSV/HSB is simple:

  l = Math.max(r, g, b); // 0 to 255

Lightness according to HSL is sort of simple too:

  max = Math.max(r, g, b);
  min = Math.min(r, g, b);
  l = (max+min) / 2; // 0 to 255

*Actual* light brightness is a bit different, and depends on color profile (this is an approximation):

  lr = 0.212671;
  lg = 0.715160;
  lb = 0.072169;
  l = (lr * r) + (lg * g) + (lb * b); // 0 to 255

The latter one is the 'right' way to measure the brightness of a color (ie, 0x00ff00 is actually much brighter than 0x0000ff). The others are mathematical representation models and doesn't reflect our perception of brightness. Choosing the correct one will depend on what you're trying to do.

References:
http://en.wikipedia.org/wiki/HSL_color_space
http://www.faqs.org/faqs/graphics/colorspace-faq

Zeh

Hans Wichman wrote:
Hi,
just guessing here, but i think converting them to HSB first might work.
Then you only need the B value.

hth
JC

On Thu, Jul 24, 2008 at 8:55 PM, Jim McIntyre <[EMAIL PROTECTED]> wrote:

Does anyone know a good formula for comparing brightness of RGB color
values?

Obviously, 0xCCCCCC is brighter than 0x333333. But one can't always infer
that a larger number is brighter than a smaller: 0x330000 is a larger
number, but much darker than, 0x00FFFF.

Would averaging (or simply adding) the three color components work, or is
it more nuanced than that?

Thanks,
Jim
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to