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%);
– tomasantunes
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.
– joncardoso