Re: [Flashcoders] Problems getting the brightness of a color returnedfrom getPixel
In other words, to make your above code to compute brightness work from a value returned by getPixel(), just change the few first line of the HSB method from: RGBtoHSB = function(rgb){ var r = rgb.r var g = rgb.g var b = rgb.b ... to: RGBtoHSB = function(var pixelvalue){ var r = pixelvalue 16 0xFF; var g = pixelvalue 8 0xFF; var b = pixelvalue 0xFF; ... ...then call it this way: var pixel = getPixel(); var hsb = RGBtoHSB(pixel); then simply use hsb.b Hope it helps explaining :-) B. --- you could even recode the method to just return the brightness alone (no Hue, no Saturation): (NOTE: this has not been tested - I simply stripped off the unnecessary code from the previous method) 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); } If I understand correctly, this should return a number from 0 to 100 representing the brightness. Which is in fact only the highest channel value. (scaled down to the 0-100 range). 2006/7/29, Zeh Fernando [EMAIL PROTECTED]: Thanks very much for your response Martin but I don't understand it. I don't have three vars called R G and B. I just have a number which was retuned from getPixel. Am I being stupid? I can't see how I should do what you sugest. Pease explain a little further. That number you're getting is a color. You're just seeing it in decimal form, rather than hexadecimal form. It's the same thing. For example, the value for white is either 16777215 or 0xff (the two are the SAME number, just represented in two different ways). On 0xff, the first ff pair is R, then G, then B. Like when you have html color, #ff. To extract the single R G and B values from a color, simply apply Martin's code to the color. var color = 0xffc1e2; // or the one returned by getPixel, same thing var r = color 16 0xFF var g = color 8 0xFF var b = color 0xFF Then R, G, and B will contain the values for each channel, going from 0 to 255. Calculating brightness from that is one whole different matter, though. - Zeh ___ Flashcoders@chattyfig.figleaf.com 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 ___ Flashcoders@chattyfig.figleaf.com 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
Re: [Flashcoders] Problems getting the brightness of a color returnedfrom getPixel
Thanks very much for your response Martin but I don't understand it. I don't have three vars called R G and B. I just have a number which was retuned from getPixel. Am I being stupid? I can't see how I should do what you sugest. Pease explain a little further. That number you're getting is a color. You're just seeing it in decimal form, rather than hexadecimal form. It's the same thing. For example, the value for white is either 16777215 or 0xff (the two are the SAME number, just represented in two different ways). On 0xff, the first ff pair is R, then G, then B. Like when you have html color, #ff. To extract the single R G and B values from a color, simply apply Martin's code to the color. var color = 0xffc1e2; // or the one returned by getPixel, same thing var r = color 16 0xFF var g = color 8 0xFF var b = color 0xFF Then R, G, and B will contain the values for each channel, going from 0 to 255. Calculating brightness from that is one whole different matter, though. - Zeh ___ Flashcoders@chattyfig.figleaf.com 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
RE: [Flashcoders] Problems getting the brightness of a color returnedfrom 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: b ...which is the same thing as this in hexadecimal: 0xFF ... 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: b 16 = b Hexadecimal: 0xFF 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 (0x7F): The binary value of the color: 0111 Split into colors: 0111 Shift right 8 bits: 10111b 8 = In hexadecimal, this result is: 0x In decimal, it is: 65535 Clearly this is too large, because it includes the red value. To remove it, we use a bitwise AND. b 0xFF = b = 0xFF To illustrate, we are taking this value: b (=0x; =65535) ...and doing a bitwise AND with this value: b (=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: b (=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 ___ Flashcoders@chattyfig.figleaf.com 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