Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. function adjustBrightness($hex, $steps) {
  2. // Steps should be between -255 and 255. Negative = darker, positive = lighter
  3. $steps = max(-255, min(255, $steps));
  4.  
  5. // Normalize into a six character long hex string
  6. $hex = str_replace('#', '', $hex);
  7. if (strlen($hex) == 3) {
  8. $hex = str_repeat(substr($hex, 0, 1), 2) . str_repeat(substr($hex, 1, 1), 2) . str_repeat(substr($hex, 2, 1), 2);
  9. }
  10.  
  11. // Split into three parts: R, G and B
  12. $color_parts = str_split($hex, 2);
  13. $return = '#';
  14.  
  15. foreach ($color_parts as $color) {
  16. $color = hexdec($color); // Convert to decimal
  17. $color = max(0, min(255, $color + $steps)); // Adjust color
  18. $return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code
  19. }
  20.  
  21. return $return;
  22.  
  23. }
  24.  
  25. // Is device a mobile?
  26. function isMobile() {
  27. 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"]);
  28. }
  29.  
  30. // Get our colors for use in the content
  31. $theme_half = hex2rgba($theme_color, 0.5);
  32. $theme_part = hex2rgba($theme_color, 0.75);
  33. $theme_full = hex2rgba($theme_color, 1);
  34.  
  35. $hex = $theme_color; //Bg color in hex, without any prefixing #!
  36.  
  37. //break up the color in its RGB components
  38. $r = hexdec(substr($hex, 0, 2));
  39. $g = hexdec(substr($hex, 2, 2));
  40. $b = hexdec(substr($hex, 4, 2));
  41.  
  42. //do simple weighted avarage
  43. //
  44. //(This might be overly simplistic as different colors are perceived
  45. // differently. That is a green of 128 might be brighter than a red of 128.
  46. // But as long as it's just about picking a white or black text color...)
  47. $header_shade = "";
  48.  
  49. if ($r + $g + $b > 382) {
  50. //bright color, use dark font
  51. $header_shade = adjustBrightness($theme_colorr, 150);
  52. echo "bright color";
  53. } else {
  54. //dark color, use bright font
  55. $header_shade = adjustBrightness($theme_colorr, 150);
  56. echo "dark color";
  57. echo $header_shade;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement