The function below takes the input stream and replaces the color values ββ$ with the saved values ββfrom the database (color1 = ffffff, color2 = aaaaaa, color3 = cccccc, etc.)
It currently performs direct detection / replacement of six-digit six-digit values. Is there a regex that I can add to my_colorReplace function to return a six-digit hexadecimal or 9-digit rgb based on the input stream pattern?
function my_colorReplace($buffer) { $buffer = str_replace(array('$color1'), '#'.get_option("my_theme_header_color").'', $buffer); $buffer = str_replace(array('$color2'), '#'.get_option("my_theme_sidebar_color").'', $buffer); $buffer = str_replace(array('$color3'), '#'.get_option("my_theme_spot_color_alt").'', $buffer); $buffer = str_replace(array('$color4'), '#'.get_option("my_theme_spot_color_alt2").'', $buffer); return $buffer; }
Utility to convert hex value to rgb
function convertHexToRGB($hexColor){ if( preg_match( '/^#?([a-h0-9]{2})([a-h0-9]{2})([a-h0-9]{2})$/i', $hexColor, $matches ) ) { return array('red' => hexdec( $matches[1] ),'green' => hexdec( $matches[2] ),'blue' => hexdec( $matches[3] )); } else { return array( 120, 120, 120 ); } }
Input stream ($ buffer):
.sidebar{ background:$color1; color:$color2; } .header{ background: linear-gradient(to bottom, rgba($color1,.5) 0%, rgba(255,255,255,0.96) 100px, rgba(255,255,255,0.95) 150px); color:rgb($color3); }
The expected return result is $ buffer (where color1 = ffffff, color2 = aaaaaa, color3 = cccccc
.sidebar{ background:#fffff; color:#aaaaaa; } .header{ background: linear-gradient(to bottom, rgba(255,255,255,.5) 0%, rgba(255,255,255,0.96) 100px, rgba(255,255,255,0.95) 150px); color:rgb(204,204,204); }