Treat all hexadecimal colors in a.css file using php and regex

Asked

Viewed 110 times

2

I don’t know if you can help me but I want to replace the color in the "#xxxxxx" format in a CSS via Regular expression and PHP.

I have a function that calculates the color and replaces it with the reverse color. I need to take CSS and invert all colors in the "#xxxxxx" format by their negative color. I don’t know the best way to do this.

i have function reversing colors ready.

    FUNCTION inverseHex( $color )
$color       = TRIM($color);
         $prependHash = FALSE;

         IF(STRPOS($color,'#')!==FALSE) {
              $prependHash = TRUE;
              $color       = STR_REPLACE('#',NULL,$color);
         }

         SWITCH($len=STRLEN($color)) {
              CASE 3:
                   $color=PREG_REPLACE("/(.)(.)(.)/","\\1\\1\\2\\2\\3\\3",$color);
              CASE 6:
                   BREAK;
              DEFAULT:
                   TRIGGER_ERROR("Invalid hex length ($len). Must be (3) or (6)", E_USER_ERROR);
         }

         IF(!PREG_MATCH('/[a-f0-9]{6}/i',$color)) {
              $color = HTMLENTITIES($color);
              TRIGGER_ERROR( "Invalid hex string #$color", E_USER_ERROR );
         }

         $r = DECHEX(255-HEXDEC(SUBSTR($color,0,2)));
         $r = (STRLEN($r)>1)?$r:'0'.$r;
         $g = DECHEX(255-HEXDEC(SUBSTR($color,2,2)));
         $g = (STRLEN($g)>1)?$g:'0'.$g;
         $b = DECHEX(255-HEXDEC(SUBSTR($color,4,2)));
         $b = (STRLEN($b)>1)?$b:'0'.$b;

         RETURN ($prependHash?'#':NULL).$r.$g.$b;

and I have regular expressions to locate colors:

 #\b\w{5}+[d]\b

But I don’t know how to use the preg_replace to scroll through the text and replace each occurrence, not by another array but passing the inverse Hex() function above, and do this across the CSS. to print the CSS in all negative colors

If anyone gives me a hint, I can already turn around to produce the code. I have to do this in 30 CSS files with thousands of lines each.

  • in a separate note, you can invert the CSS colors with the property filter: invert(100%);

  • Thanks friend, really this meets, but would have maintenance problem because it would always have to put negative colors of new properties. so I went to a more radical action.

2 answers

2

I was able to solve the problem with the function preg_replace_callback, just insert my COLORS function into create_function() for callback. Thank you all.

1


I am entering the complete code of the solution to the problem I had, if it helps other people.

<?php

//Função em PHP que irá alterar a cor para sua forma negativa
//Gerando uma saída com o arquivo CSS prontinho pra ser salvo.

FUNCTION inverseHex( $color )
{


     $color       = TRIM($color);
     $prependHash = FALSE;

     IF(STRPOS($color,'#')!==FALSE) {
          $prependHash = TRUE;
          $color       = STR_REPLACE('#',NULL,$color);
     }

     SWITCH($len=STRLEN($color)) {
          CASE 3:
               $color=PREG_REPLACE("/(.)(.)(.)/","\\1\\1\\2\\2\\3\\3",$color);
          CASE 6:
               BREAK;
          DEFAULT:
               TRIGGER_ERROR("Invalid hex length ($len). Must be (3) or (6)", E_USER_ERROR);
     }

     IF(!PREG_MATCH('/[a-f0-9]{6}/i',$color)) {
          $color = HTMLENTITIES($color);
          TRIGGER_ERROR( "Invalid hex string #$color", E_USER_ERROR );
     }

     $r = DECHEX(255-HEXDEC(SUBSTR($color,0,2)));
     $r = (STRLEN($r)>1)?$r:'0'.$r;
     $g = DECHEX(255-HEXDEC(SUBSTR($color,2,2)));
     $g = (STRLEN($g)>1)?$g:'0'.$g;
     $b = DECHEX(255-HEXDEC(SUBSTR($color,4,2)));
     $b = (STRLEN($b)>1)?$b:'0'.$b;

     RETURN ($prependHash?'#':NULL).$r.$g.$b;

}

$str = $textoDoArquivoCSS//arquivo de texto, no caso eu fiz por Curl.

function inverseHexRegex($elements) {

   return inverseHex($elements[1]);
}
$str = preg_replace_callback('/(#[a-f0-9]{3,6})/i', 'inverseHexRegex', $str);

echo $str;

?>

With this output, I only save the file and is already ready the negative version of it. Note: this code will not treat colors expressed in words: black, red, etc... nor colors in RBG: rgb(xxx,xxx,xxx).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.