Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function adjustBrightness($hex, $steps) {
- // Steps should be between -255 and 255. Negative = darker, positive = lighter
- $steps = max(-255, min(255, $steps));
- // Normalize into a six character long hex string
- $hex = str_replace('#', '', $hex);
- if (strlen($hex) == 3) {
- $hex = str_repeat(substr($hex, 0, 1), 2) . str_repeat(substr($hex, 1, 1), 2) . str_repeat(substr($hex, 2, 1), 2);
- }
- // Split into three parts: R, G and B
- $color_parts = str_split($hex, 2);
- $return = '#';
- foreach ($color_parts as $color) {
- $color = hexdec($color); // Convert to decimal
- $color = max(0, min(255, $color + $steps)); // Adjust color
- $return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code
- }
- return $return;
- }
- // Is device a mobile?
- function isMobile() {
- return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
- }
- // Get our colors for use in the content
- $theme_half = hex2rgba($theme_color, 0.5);
- $theme_part = hex2rgba($theme_color, 0.75);
- $theme_full = hex2rgba($theme_color, 1);
- $hex = $theme_color; //Bg color in hex, without any prefixing #!
- //break up the color in its RGB components
- $r = hexdec(substr($hex, 0, 2));
- $g = hexdec(substr($hex, 2, 2));
- $b = hexdec(substr($hex, 4, 2));
- //do simple weighted avarage
- //
- //(This might be overly simplistic as different colors are perceived
- // differently. That is a green of 128 might be brighter than a red of 128.
- // But as long as it's just about picking a white or black text color...)
- $header_shade = "";
- if ($r + $g + $b > 382) {
- //bright color, use dark font
- $header_shade = adjustBrightness($theme_colorr, 150);
- echo "bright color";
- } else {
- //dark color, use bright font
- $header_shade = adjustBrightness($theme_colorr, 150);
- echo "dark color";
- echo $header_shade;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement