function hoot_sanitize_hex($hex, $default = '') { if ($hex = hoot_color_santize_hex($hex)) { return $hex; } if (!is_array($default)) { if ($default = hoot_color_santize_hex($default)) { return $default; } } return null; }
/** * Check if a hexa color value is a 'dark' color * * @since 1.0.0 * @access public * @param string $hex - hex color value to be manipulated * @return bool */ public function isdark($hex) { $hex = hoot_color_santize_hex($hex); // Returns sanitized color with '#' if (empty($hex)) { return null; } $redhex = substr($hex, 1, 2); $greenhex = substr($hex, 3, 2); $bluehex = substr($hex, 5, 2); // $var_r, $var_g and $var_b are the three decimal fractions to be input to our RGB-to-HSL conversion routine $var_r = hexdec($redhex) / 255; $var_g = hexdec($greenhex) / 255; $var_b = hexdec($bluehex) / 255; // Input is $var_r, $var_g and $var_b from above // Output is HSL equivalent as $h, $s and $l — these are expressed as fractions of 1, like the input values $var_min = min($var_r, $var_g, $var_b); $var_max = max($var_r, $var_g, $var_b); $del_max = $var_max - $var_min; $l = ($var_max + $var_min) / 2; if ($del_max == 0) { $h = 0; $s = 0; } else { if ($l < 0.5) { $s = $del_max / ($var_max + $var_min); } else { $s = $del_max / (2 - $var_max - $var_min); } $del_r = (($var_max - $var_r) / 6 + $del_max / 2) / $del_max; $del_g = (($var_max - $var_g) / 6 + $del_max / 2) / $del_max; $del_b = (($var_max - $var_b) / 6 + $del_max / 2) / $del_max; if ($var_r == $var_max) { $h = $del_b - $del_g; } elseif ($var_g == $var_max) { $h = 1 / 3 + $del_r - $del_b; } elseif ($var_b == $var_max) { $h = 2 / 3 + $del_g - $del_r; } if ($h < 0) { $h += 1; } if ($h > 1) { $h -= 1; } } //So now we have the colour as an HSL value, in the variables $h, $s and $l. These three output variables are again held as fractions of 1 at this stage, rather than as degrees and percentages. So e.g., cyan (180° 100% 50%) would come out as $h = 0.5, $s = 1, and $l = 0.5. if ($l < 0.5) { return true; } else { return false; } }
/** * Create CSS styles from a typography array. * * @since 1.1.1 * @access public * @param array $typography * @param bool $reset Reset earlier css rules from stylesheets etc. * @return string */ function hoot_css_typography($typography, $reset = false) { $typography_defaults = array('size' => '', 'face' => '', 'style' => '', 'color' => ''); $typography = wp_parse_args($typography, $typography_defaults); extract($typography, EXTR_SKIP); $output = ''; if (!empty($color)) { $color = hoot_color_santize_hex($color); if ($color) { $output .= hoot_css_rule('color', $color); } } if (!empty($size)) { $output .= hoot_css_rule('font-size', $size); } if (!empty($face)) { $output .= hoot_css_rule('font-family', $face); } if (!empty($style)) { switch ($style) { case 'italic': $output .= hoot_css_rule('font-style', 'italic'); if ($reset) { $output .= hoot_css_rule('text-transform', 'none'); } if ($reset) { $output .= hoot_css_rule('font-weight', 'normal'); } break; case 'bold': $output .= hoot_css_rule('font-weight', 'bold'); if ($reset) { $output .= hoot_css_rule('text-transform', 'none'); } if ($reset) { $output .= hoot_css_rule('font-style', 'normal'); } break; case 'bold italic': $output .= hoot_css_rule('font-weight', 'bold'); $output .= hoot_css_rule('font-style', 'italic'); if ($reset) { $output .= hoot_css_rule('text-transform', 'none'); } break; case 'lighter': $output .= hoot_css_rule('font-weight', 'lighter'); if ($reset) { $output .= hoot_css_rule('text-transform', 'none'); } if ($reset) { $output .= hoot_css_rule('font-style', 'normal'); } break; case 'lighter italic': $output .= hoot_css_rule('font-weight', 'lighter'); $output .= hoot_css_rule('font-style', 'italic'); if ($reset) { $output .= hoot_css_rule('text-transform', 'none'); } break; case 'uppercase': $output .= hoot_css_rule('text-transform', 'uppercase'); if ($reset) { $output .= hoot_css_rule('font-weight', 'normal'); } if ($reset) { $output .= hoot_css_rule('font-style', 'normal'); } break; case 'uppercase italic': $output .= hoot_css_rule('text-transform', 'uppercase'); $output .= hoot_css_rule('font-style', 'italic'); if ($reset) { $output .= hoot_css_rule('font-weight', 'normal'); } break; case 'uppercase bold': $output .= hoot_css_rule('text-transform', 'uppercase'); $output .= hoot_css_rule('font-weight', 'bold'); if ($reset) { $output .= hoot_css_rule('font-style', 'normal'); } break; case 'uppercase bold italic': $output .= hoot_css_rule('text-transform', 'uppercase'); $output .= hoot_css_rule('font-weight', 'bold'); $output .= hoot_css_rule('font-style', 'italic'); break; case 'uppercase lighter': $output .= hoot_css_rule('text-transform', 'uppercase'); $output .= hoot_css_rule('font-weight', 'lighter'); if ($reset) { $output .= hoot_css_rule('font-style', 'normal'); } break; case 'uppercase lighter italic': $output .= hoot_css_rule('text-transform', 'uppercase'); $output .= hoot_css_rule('font-weight', 'lighter'); $output .= hoot_css_rule('font-style', 'italic'); break; case 'none': default: if ($reset) { $output .= hoot_css_rule('text-transform', 'none'); } if ($reset) { $output .= hoot_css_rule('font-weight', 'normal'); } if ($reset) { $output .= hoot_css_rule('font-style', 'normal'); } break; } } return $output; }