Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自动转换CSS文件以在深色背景上使用_Php_Css_Algorithm - Fatal编程技术网

Php 自动转换CSS文件以在深色背景上使用

Php 自动转换CSS文件以在深色背景上使用,php,css,algorithm,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:

我有大约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文件中的颜色设计为仅在浅色(最好是白色)背景下看起来正常。是否有一个algo(我可以用PHP代码表达)可以应用于这些文件中的颜色,使它们在深色背景下看起来更漂亮(接近黑色)?也许我应该把所有的颜色都倒过来?或者有更好的方法吗?

如果CSS文件包含一些颜色集,您可以先为每个可用颜色定义新颜色。
然后使用
file\u get\u contents
函数将完整的CSS文件读入变量。应用
str\u replace
以使用新值重新拼贴所有颜色,最后使用
file\u put\u contents
写回文件。(用另一个名字?)


要处理200个CSS文件,请使用
opendir
readdir
closedir
读取包含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)));

查看更多关于如何递归地递归目录(注释部分)。

不适用于现有代码,但是对于长期使用,考虑CSS抽象-更少()和SASS()是2个例子。这将允许您在以后处理和重新皮肤您的CSS问题,即CSS文件是由外部库生成的,我不希望修改。