背景图片深色还是浅色?

发布于 2024-11-04 03:23:28 字数 265 浏览 7 评论 0原文

我正在使用 PHP 进行 ODP->HTML 转换。我有以下问题:

使用样式:use-window-font-color 属性来指定是否 窗口前景色应该是 用作前景色 浅色背景颜色和白色 深色背景颜色。

(OpenDocument 规范版本 1.0、15.4.4)

如果我有背景图像,我如何检查该图像是亮还是暗?

你有什么想法吗?

提前致谢, 来雾岛

I'm doing a ODP->HTML conversion with PHP. I have problems with the following:

Use the style:use-window-font-color
property to specify whether or not the
window foreground color should be as
used as the foreground color for a
light background color and white for a
dark background color.

(OpenDocument specification version 1.0, 15.4.4)

If i have a background image, how do i check, if this image is light or dark?

Do you have any ideas?

Thanks in advance,
Levu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

谜兔 2024-11-11 03:23:28

我认为这是一个非常有趣的问题需要解决,所以我编写了一个快速脚本来完成它。遵循提供的其他建议

<?php

    // TODO supply your own filenames
    $filenames = array(
        'testpics/client-bella-vi.jpg',
        'testpics/istockphoto_8577991-concept-of-business-people-crowd.jpg',
        'testpics/medium-gray.jpg');

    // loop though each file
    foreach ($filenames as $filename) {

        echo "$filename<br/>";

        $luminance = get_avg_luminance($filename,10);
        echo "AVG LUMINANCE: $luminance<br />";

        // assume a medium gray is the threshold, #acacac or RGB(172, 172, 172)
        // this equates to a luminance of 170
        if ($luminance > 170) {
            echo "Black Text<br />";
        } else {
            echo 'White Text<br />';
        }

        echo "<br />";
    }
    exit;

    // get average luminance, by sampling $num_samples times in both x,y directions
    function get_avg_luminance($filename, $num_samples=10) {
        $img = imagecreatefromjpeg($filename);

        $width = imagesx($img);
        $height = imagesy($img);

        $x_step = intval($width/$num_samples);
        $y_step = intval($height/$num_samples);

        $total_lum = 0;

        $sample_no = 1;

        for ($x=0; $x<$width; $x+=$x_step) {
            for ($y=0; $y<$height; $y+=$y_step) {

                $rgb = imagecolorat($img, $x, $y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;

                // choose a simple luminance formula from here
                // http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
                $lum = ($r+$r+$b+$g+$g+$g)/6;

                $total_lum += $lum;

                // debugging code
     //           echo "$sample_no - XY: $x,$y = $r, $g, $b = $lum<br />";
                $sample_no++;
            }
        }

        // work out the average
        $avg_lum  = $total_lum/$sample_no;

        return $avg_lum;
    }

I thought this was quite an interesting problem to solve, so i hacked up a quick script to do it. following the other suggestions provided

<?php

    // TODO supply your own filenames
    $filenames = array(
        'testpics/client-bella-vi.jpg',
        'testpics/istockphoto_8577991-concept-of-business-people-crowd.jpg',
        'testpics/medium-gray.jpg');

    // loop though each file
    foreach ($filenames as $filename) {

        echo "$filename<br/>";

        $luminance = get_avg_luminance($filename,10);
        echo "AVG LUMINANCE: $luminance<br />";

        // assume a medium gray is the threshold, #acacac or RGB(172, 172, 172)
        // this equates to a luminance of 170
        if ($luminance > 170) {
            echo "Black Text<br />";
        } else {
            echo 'White Text<br />';
        }

        echo "<br />";
    }
    exit;

    // get average luminance, by sampling $num_samples times in both x,y directions
    function get_avg_luminance($filename, $num_samples=10) {
        $img = imagecreatefromjpeg($filename);

        $width = imagesx($img);
        $height = imagesy($img);

        $x_step = intval($width/$num_samples);
        $y_step = intval($height/$num_samples);

        $total_lum = 0;

        $sample_no = 1;

        for ($x=0; $x<$width; $x+=$x_step) {
            for ($y=0; $y<$height; $y+=$y_step) {

                $rgb = imagecolorat($img, $x, $y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;

                // choose a simple luminance formula from here
                // http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
                $lum = ($r+$r+$b+$g+$g+$g)/6;

                $total_lum += $lum;

                // debugging code
     //           echo "$sample_no - XY: $x,$y = $r, $g, $b = $lum<br />";
                $sample_no++;
            }
        }

        // work out the average
        $avg_lum  = $total_lum/$sample_no;

        return $avg_lum;
    }
雨落□心尘 2024-11-11 03:23:28

您可以使用某种图像处理算法来检查像素亮度并计算平均图像亮度。

本文档将帮助您入门:

http://www.kweii.com/site/ color_theory/2007_LV/BrightnessCalculation.pdf

you could possibly use some image processing algorithm that would examine the pixel brightness and calculate mean image brightness.

this document will get you started:

http://www.kweii.com/site/color_theory/2007_LV/BrightnessCalculation.pdf

客…行舟 2024-11-11 03:23:28

如果您想使用 GD,请尝试使用 imagecolorat< /a> 对图像的像素进行采样。您可以确定颜色的 RGB,如 PHP 手册页所示。

接下来,获取 RGB 样本并使用基本亮度公式确定其亮度。

确定您认为浅色与深色的阈值,并相应地进行分类。

If you're looking to use GD, trying using imagecolorat to sample pixels of the image. You can determine the RGB of the color as shown on the PHP man page.

Next, take the RGB sample(s) and determine their brightness using a basic luminance formula.

Determine a threshold for what you consider light vs. dark and categorize accordingly.

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