确定 getPixel() 值是否大于或小于 50% 灰度
我正在尝试循环遍历位图并使用 getPixel() 确定每个像素是否比灰色更亮或更暗。问题是,我不确定如何判断 getPixel() 返回的值是比灰色更暗还是更亮。
中性灰色约为0x808080或R:127,G:127,B:127。我需要如何修改下面的代码才能准确确定这一点?
for (var dx:int=0; dx < objectWidth; dx++)
{
for (var dy:int=0; dy < objectHeight; dy++)
{
if (testBmd.getPixel(dx, dy) > GRAY)
{
trace("Lighter than gray!");
} else {
trace("Darker than gray!");
}
}
}
I am trying to loop through a bitmap and determine if each pixel is lighter or darker than gray using getPixel(). Problem is, I am not sure how to tell whether the value returned by getPixel() is darker or lighter than gray.
Neutral gray is about 0x808080 or R:127, G:127, B:127. How would I need to modify the code below to accurately determine this?
for (var dx:int=0; dx < objectWidth; dx++)
{
for (var dy:int=0; dy < objectHeight; dy++)
{
if (testBmd.getPixel(dx, dy) > GRAY)
{
trace("Lighter than gray!");
} else {
trace("Darker than gray!");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
扩展 Adam 的答案 再进一步,您可以使用这样的函数生成亮度值...
然后您可以像这样测试 50% 的灰度阈值:
To extend Adam's answer a bit further, you could generate a luminance value using a function like this...
Then you can test for your 50% grey threshold like this:
亮度就是答案 - 这里需要数学和解释:
http://www.scantips.com/lumin.html
你知道如何继续:)
编辑:
在 livedocs (livedocs - BitmapData - getPixel32()),您可以在示例中看到他们如何从 getPixel32() 返回值获取 r、g、b 值。也许你可以使用 i:]
另外,理查德的 答案 看起来它已经满足了您的需求,尽管如果您将其与上面的示例结合起来 - 瞧 - 您已经得到了亮度比较:]
Luminance is the answer - Math needed and explanation here:
http://www.scantips.com/lumin.html
you know how to continue :)
Edit:
on livedocs (livedocs - BitmapData - getPixel32()), you can see in example, how they get r,g,b, values from getPixel32() return value. Maybe you can use i:]
Also, Richard's answer looks like it already does what you need, although if you combine it with example from above - voilla - you've got yourself an luminance comparison :]