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

2

I do not know if they can help me but I want to replace the color in "#xxxxxx" format in a CSS through Regular Expression and PHP.

I have a function that calculates the color and replaces it with the inverse color. I need to get the CSS and invert all colors in the "#xxxxxx" format by their negative color. I do not know how best to do this.

I have the function that reverses the 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 the regular expressions to find the colors:

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

But I do not know how to use preg_replace to scroll through the text and replace each instance, not with another array but passing the inverseHex () function above, and do this throughout the CSS. to print CSS with all negative colors

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

    
asked by anonymous 24.12.2016 / 03:44

2 answers

1

I'm inserting the complete solution code for the problem I had, if you help 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 just saved the file and the negative version is already ready. Note: this code will not treat colors expressed in words: black, red, etc ... nor colors in RBG: rgb (xxx, xxx, xxx).

    
28.12.2016 / 18:32
2

I was able to resolve the issue with the preg_replace_callback , I just entered my COLORS function in create_function () for callback . Thank you all.

    
27.12.2016 / 16:06