确定 getPixel() 值是否大于或小于 50% 灰度

发布于 2024-08-27 02:06:07 字数 475 浏览 3 评论 0原文

我正在尝试循环遍历位图并使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别在捏我脸啦 2024-09-03 02:06:07

扩展 Adam 的答案 再进一步,您可以使用这样的函数生成亮度值...

function luminance(myRGB:int):int {
//returns a luminance value between 0 and 255
var R:int = (myRGB / 65536) % 256;
var G:int = (myRGB / 256) % 256;
var B:int = myRGB % 256;
return ((0.3*R)+(0.59*G)+(0.11*B));
}

然后您可以像这样测试 50% 的灰度阈值:

if (luminance(testBmd.getPixel(dx, dy)) > 127)

To extend Adam's answer a bit further, you could generate a luminance value using a function like this...

function luminance(myRGB:int):int {
//returns a luminance value between 0 and 255
var R:int = (myRGB / 65536) % 256;
var G:int = (myRGB / 256) % 256;
var B:int = myRGB % 256;
return ((0.3*R)+(0.59*G)+(0.11*B));
}

Then you can test for your 50% grey threshold like this:

if (luminance(testBmd.getPixel(dx, dy)) > 127)
一世旳自豪 2024-09-03 02:06:07

亮度就是答案 - 这里需要数学和解释:

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 :]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文