php - 自动转换 CSS 文件以用于深色背景

标签 php css algorithm

我有大约 200 个这样的 CSS 文件:

/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for bnf
 * CSS class: , CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.bnf .de1, .bnf .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.bnf  {font-family:monospace;}
.bnf .imp {font-weight: bold; color: red;}
.bnf li, .bnf .li1 {font-weight: normal; vertical-align:top;}
.bnf .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.bnf .li2 {font-weight: bold; vertical-align:top;}
.bnf .sy0 {color: #000066; font-weight: bold;}
.bnf .st0 {color: #a00;}
.bnf .st1 {color: #a00;}
.bnf .re0 {color: #007;}
.bnf .ln-xtra, .bnf li.ln-xtra, .bnf div.ln-xtra {background-color: #ffc;}
.bnf span.xtra { display:block; }

但是这些 CSS 文件中的颜色被设计为仅在浅色(最好是白色)背景下看起来不错。是否有算法(我可以用 PHP 代码表达)我可以应用于这些文件中的颜色,使它们在深色背景(接近黑色)上看起来不错?也许我应该反转所有颜色?或者有更好的方法吗?

最佳答案

要将从 CSS 文件中找到的所有颜色值转换为其反值,您可以使用此函数:

function inverseColors($css) {
    preg_match_all('/#([a-f0-9]{6}|[a-f0-9]{3})/i', $css, $matches);
    $original = $matches[0];
    $inversed = array();
    foreach($matches[1] as $key => $color) {    
        $parts = str_split($color, strlen($color) == 3 ? 1 : 2);
        foreach($parts as &$part) {
            $part = str_pad(dechex(255 - hexdec($part)), 2, 0, STR_PAD_LEFT);
        }
        $inversed[$key] = '#'.implode('', $parts);    
    }

    $css = str_replace($original, $inversed, $css);
    echo $css;
}

这将适用于三位和六位十六进制颜色值。但请注意,这不会产生最佳颜色,反色可能不是您布局的最佳选择。通过为所有颜色创建一个查找表并在新 CSS 中替换这些值,可以获得更好的结果。

要遍历所有 CSS 文件,您可以使用 SPL 类进行递归搜索,然后使用替换 CSS 文件

file_put_contents($file, inverseColors(file_get_contents($file)));

看看PHP's documentation about glob了解更多如何递归迭代目录(在评论部分)。

关于php - 自动转换 CSS 文件以用于深色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2040848/

相关文章:

r - 使用 Polar 方法模拟随机变量

java - java和PHP之间使用static的区别?

html - 将img保留在页面底部

java - 更改算法

html 表单和负顶部 css 值

css - 分页媒体@page :nth selector

python - 在 Python 中查找字符串是否与单词、短语、 bool AND 列表中的任何术语匹配的最快方法是什么?

php - 我想对两个表求和,然后将两个值相加以获得收入

php - 选择时间与 future 日期相同的数据

php - Symfony 控制台选项卡补全