hum, I guess you missed my last post(?)
Here's the code again:
function computeBrightness(pixelvalue)
{
var r = pixelvalue >> 16 & 0xFF;
var g = pixelvalue >> 8 & 0xFF;
var b = pixelvalue & 0xFF;
var bright = Math.max(Math.max(r,g),b);
return Math.round((bright/255)*100);
}
And for an "optimized/faster" version, you could try something like:
function computeBrightness(pixelvalue)
{
// returns the larger value of r, g or b -- and scale it down to a 0..100
range.
var r = pixelvalue >> 16 & 0xFF;
var g = pixelvalue >> 8 & 0xFF;
var b = pixelvalue & 0xFF;
var bright = (r>g)?((r>b)?r:b):((g>b)?g:b);
return (bright/255)*100; // no rounding.
}
(Again, this is untested code -- but it looks like it should work).
Bernard
2006/7/30, James Deakin <[EMAIL PROTECTED]>:
HI Guys, Thanks very much to you all for your help and explanations. I
understood that the number represented a colour value but I now have a
much better understanding of how it does so.
In your opinion what is the most efficient way to retrieve the
relative brightness of that colour.
What I need is a value between 0 and 100 where 0 is black and 100 is
white and the numbers in-between represent the shades in-between.
What I am going to try is this.
Split the number into its components RGB
use this code which came from the Flash API project
//colorModel converter RGB->HSB
//returns a hsb object
RGBtoHSB = function(rgb){
var r = rgb.r
var g = rgb.g
var b = rgb.b
var hsb = new Object();
hsb.b = Math.max(Math.max(r,g),b);
var min = Math.min(Math.min(r,g),b);
hsb.s = (hsb.b <= 0) ? 0 : Math.round (100*(hsb.b - min)/hsb.b);
hsb.b = Math.round((hsb.b /255)*100);
hsb.h = 0;
if((r == g) && (g == b)){
hsb.h = 0;
} else if(r >= g && g >= b){
hsb.h = 60*(g-b)/(r-b);
} else if(g >= r && r >= b){
hsb.h = 60 + 60*(g-r)/(g-b);
} else if(g >= b && b >= r){
hsb.h = 120 + 60*(b-r)/(g-r);
} else if(b >= g && g >= r){
hsb.h = 180 + 60*(b-g)/(b-r);
} else if(b >= r && r >= g){
hsb.h = 240 + 60*(r-g)/(b-g);
} else if(r >= b && b >= g){
hsb.h = 300 + 60*(r-b)/(r-g);
} else{
hsb.h = 0;
}
hsb.h = Math.round(hsb.h);
return hsb;
}
to turn it into an object with three values hue saturation and
brightness and then just make use of the brightness.
If there is a better way especially a more efficient way I would
really like to know.
On 7/29/06, Mike <[EMAIL PROTECTED]> wrote:
> Small correction.
>
> This:
> "This compares each bit in the first number to each bit in the
> second
> number. If both bits are 1 (on), that bit is 1 (on) in the result. If
> both bits are 0 (off), both bits are 0 (off) in the result. So the
> result is:"
>
> ...should be:
> "This compares each bit in the first number to each bit in the
> second
> number. If both bits are 1 (on), that bit is 1 (on) in the result. If
> *either bit is* 0 (off), *that bit is* 0 (off) in the result. So the
> result is:"
> (emphasis added)
> --
> T. Michael Keesey
>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Mike
> Sent: Saturday, July 29, 2006 3:25 PM
> To: 'Flashcoders mailing list'
> Subject: RE: [Flashcoders] Problems getting the brightness of a
> colorreturnedfrom getPixel
>
> You seem to be thinking of numbers as if they are stored like strings.
> They aren't.
>
> RGB colors are stored as 3-byte (24-bit) numbers.
>
> For example, red looks like this in binary:
>
> 111111110000000000000000b
>
> ...which is the same thing as this in hexadecimal:
>
> 0xFF0000
>
> ... which is the same thing as this in decimal:
>
> 16711680
>
> To isolate, for example the red portion, you can use SHIFT RIGHT (>>) to
> shift all bits to the "right" by 16 bits. Binary:
>
> 111111110000000000000000b >> 16 = 11111111b
>
> Hexadecimal:
>
> 0xFF0000 >> 16 = 0xFF
>
> Decimal:
>
> 16711680 >> 16 = 255
>
> Generally it's a good idea not to presume that there may not be more
> bits to the left, so you can filter them out using a bitwise AND (&). To
> explain, this better, here's how to extract the green value from bright
> cyan (0xFFFF7F):
>
> The binary value of the color:
>
> 111111111111111101111111
>
> Split into colors:
>
> 11111111 11111111 01111111
>
> Shift right 8 bits:
>
> 1111111111111111101111111b >> 8 = 1111111111111111
>
> In hexadecimal, this result is:
>
> 0xFFFF
>
> In decimal, it is:
>
> 65535
>
> Clearly this is too large, because it includes the red value. To remove
> it, we use a bitwise AND.
>
> 1111111111111111b & 0xFF = 11111111b = 0xFF
>
> To illustrate, we are taking this value:
>
> 1111111111111111b (=0xFFFF; =65535)
>
> ...and doing a bitwise AND with this value:
>
> 0000000011111111b (=0x00FF; =255)
>
> This compares each bit in the first number to each bit in the second
> number. If both bits are 1 (on), that bit is 1 (on) in the result. If
> both bits are 0 (off), both bits are 0 (off) in the result. So the
> result is:
>
> 0000000011111111b (=0xFF; =255)
>
> ...which is, indeed, the green value of the color.
>
> So Martin Wood's example (slightly edited):
>
> var r:Number = color >> 16 & 0xFF;
> var g:Number = color >> 8 & 0xFF;
> var b:Number = color & 0xFF;
>
> ... is how to retrieve the red, green, and blue values from a single RGB
> color number.
> --
> T. Michael Keesey
>
>
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com