Php 用更少的颜色反转所有颜色

Php 用更少的颜色反转所有颜色,php,css,colors,less,Php,Css,Colors,Less,我有一个我很满意的样式表,我使用以下代码编译它。效果很好 function css($inputFile, $outputFile) { $cacheFile = $inputFile.".cache"; $cache = file_exists($cacheFile) ? unserialize(file_get_contents($cacheFile)) : $inputFile; $less = new lessc; $l

我有一个我很满意的样式表,我使用以下代码编译它。效果很好

function css($inputFile, $outputFile)
{
    $cacheFile = $inputFile.".cache";

    $cache = file_exists($cacheFile)
        ? unserialize(file_get_contents($cacheFile))
        : $inputFile;

    $less = new lessc;
    $less->setFormatter("compressed");
    $newCache = $less->cachedCompile($cache);

    if ( ! file_exists($outputFile) || ! is_array($cache) || $newCache["updated"] > $cache["updated"])
    {
        file_put_contents($cacheFile, serialize($newCache));
        file_put_contents($outputFile, $newCache['compiled']);
    }
}
我有没有一个简单的方法来产生一个版本的样式表与绝对所有定义的颜色倒置?你能用更少的编译器做些新奇的事情吗

我对此感到好奇的原因是,当我反转一个屏幕截图时,这个网站看起来很不错,如果有一个便宜的方法来生成一个亮对暗版本的网站,这将是很酷的,因为这个网站目前是亮对暗的


有什么好主意吗?

您可以简单地扩展
lessc
类并修改编译器,使所有颜色都反转

我们添加了一个新类(例如
lessc\u invert
),它扩展了类
lessc
,现在我们只需要:

  • 定义一个简单的颜色反转函数:
    invert_color($c){return abs($c-255);}
  • 并覆盖
    lessc
    中的
    受保护函数compileValue($value)
    ,其中我们:
    • 'raw\u color'
      类型的值强制为
      'color'
    • 'keyword'
      类型的值强制为
      'color'
      (如果在
      $cssColors
      数组中找到)
    • 然后使用
      invert_color()
      函数反转颜色的
      $r、$g、$b
      分量
大概是这样的:

class lessc_invert extends lessc {
  protected function invert_color($c){
    return abs($c - 255);
  }
  protected function compileValue($value) {
    switch ($value[0]) {
    case 'list':
      return implode($value[1], array_map(array($this, 'compileValue'), $value[2]));
    case 'raw_color':
      return $this->compileValue($this->coerceColor($value));
    case 'keyword':
      if (isset(self::$cssColors[$value[1]])) {
        return $this->compileValue($this->coerceColor($value));
      }
      return $value[1];
    case 'number':
      list(, $num, $unit) = $value;
      if ($this->numberPrecision !== null) {
        $num = round($num, $this->numberPrecision);
      }
      return $num . $unit;
        case 'string':
      list(, $delim, $content) = $value;
      foreach ($content as &$part) {
        if (is_array($part)) {
          $part = $this->compileValue($part);
        }
      }
      return $delim . implode($content) . $delim;
    case 'color':
      list(, $r, $g, $b) = $value;
      $r = $this->invert_color(round($r));
      $g = $this->invert_color(round($g));
      $b = $this->invert_color(round($b));
      if (count($value) == 5 && $value[4] != 1) {
        return 'rgba('.$r.','.$g.','.$b.','.$value[4].')';
      }
      $h = sprintf("#%02x%02x%02x", $r, $g, $b);
      if (!empty($this->formatter->compressColors)) {
        if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) {
          $h = '#' . $h[1] . $h[3] . $h[5];
        }
      }      
      return $h;
    case 'function':
      list(, $name, $args) = $value;
      return $name.'('.$this->compileValue($args).')';
    default:
      $this->throwError("unknown value type: $value[0]");
    }
  }
}
现在,我们可以使用新类
lessc\u invert
,而不是实例化
lessc
,如下所示:

$less = new lessc_invert;
$less->setFormatter("compressed");
$newCache = $less->cachedCompile($cache);
这应该可以奏效=)


例如:

echo $less->compile("a { color: blue; }");
现在返回:

a{color:#ff0;}

检查这个:你必须知道你的颜色变量,并重新定义它们,使色调旋转180度。@helderdarocha是的,这似乎是可行的。挑战在于以某种方式自动完成。最好不编辑现有的
less
文件。如果所有颜色都在变量中,可以使用
less.modifyVars()
替换它们。您还可以使用PHP生成反转颜色,只需将变量替换为字符串即可。只有少数颜色在变量中,其他颜色是基于这些颜色计算的。这在使用深色时效果很好,但如果我使用浅色,基本上所有的颜色都是白色:)看起来确实很有希望!今天晚些时候我会试试:)完全奏效了!令人惊叹的。知道如何不反转阴影吗?要执行特定于属性的操作,还需要包括
compileProp()
方法,这里我在
compileProp()中添加了一个开关
$f
,并根据
compileProp()中的属性
$name
更改此开关
$this->f=preg\u匹配(“/shadow/”,$name)?0 : 1;,因此如果属性是阴影(框阴影或文本阴影),颜色不会反转,但当然可以根据需要修改此属性。