static function contrastColors($string, $background) { $background = Color::StringToRgb24($background); return preg_replace_callback('/(?<!\\$)((?:\\$\\$)*)(\\$[0-9a-f][^\\$]{0,2})/iu', function ($matches) use($background) { $color = Color::StringToRgb24($matches[2]); $color = Color::Contrast($color, $background); $color = Color::Rgb24ToRgb12($color); $color = Color::Rgb12ToString($color); return $matches[1] . '$' . $color; }, $string); }
function __toString() { if ($this->style) { $styles = ''; if ($this->style & StyleParser::COLORED) { if (StyleParser::getBackground() !== null && StyleParser::getContrast() !== null) { $color = Color::Rgb12ToRgb24($this->style & 0xfff); $color = Color::Contrast($color, StyleParser::getBackground(), StyleParser::getContrast()); $color = Color::Rgb24ToString($color); } else { $color = Color::Rgb12ToString($this->style & 0xfff); } $styles .= 'color:#' . $color . ';'; } if ($this->style & StyleParser::ITALIC) { $styles .= 'font-style:italic;'; } if ($this->style & StyleParser::BOLD) { $styles .= 'font-weight:bold;'; } if ($this->style & StyleParser::SHADOWED) { $styles .= 'text-shadow:1px 1px 1px rgba(0,0,0,.5);'; } if ($this->style & StyleParser::CAPITALIZED) { $this->text = strtoupper($this->text); } if ($this->style & StyleParser::WIDE) { $styles .= 'letter-spacing:.1em;font-size:105%;'; } else { if ($this->style & StyleParser::NARROW) { $styles .= 'letter-spacing:-.1em;font-size:95%;'; } } return '<span style="' . $styles . '">' . htmlentities($this->text, ENT_QUOTES, 'UTF-8') . '</span>'; } else { return htmlentities($this->text, ENT_QUOTES, 'UTF-8'); } }