'Navy', '#1267D2' => 'Bright blue', '#0F8B8D' => 'Teal', '#6F42C1' => 'Purple', '#D97706' => 'Amber', '#BE123C' => 'Crimson', '#475569' => 'Slate', '#111827' => 'Black', '#FFFFFF' => 'White'); } /** "#abc" or "abc123" → "#AABBCC"; anything else → ''. */ function azlinkpro_colour_hex($value) { $v = ltrim(trim((string) $value), '#'); if (preg_match('/^[0-9a-fA-F]{3}$/D', $v)) { $v = $v[0] . $v[0] . $v[1] . $v[1] . $v[2] . $v[2]; } return preg_match('/^[0-9a-fA-F]{6}$/D', $v) ? '#' . strtoupper($v) : ''; } /** Dark or white text, whichever reads better on $colour (WCAG contrast). */ function azlinkpro_colour_text_on($colour) { $hex = ltrim(azlinkpro_colour_hex($colour) ?: '#FFFFFF', '#'); $channels = array(); foreach (array(0, 2, 4) as $offset) { $c = hexdec(substr($hex, $offset, 2)) / 255; $channels[] = $c <= 0.04045 ? $c / 12.92 : pow(($c + 0.055) / 1.055, 2.4); } $luminance = $channels[0] * 0.2126 + $channels[1] * 0.7152 + $channels[2] * 0.0722; return (($luminance + 0.05) / (0.0185 + 0.05)) >= (1.05 / ($luminance + 0.05)) ? '#152238' : '#ffffff'; } /** * Prints the picker. $args: id, name, value, swatches (hex => label), reset_label and reset_value * (a button that puts back the design's own colour; reset_value '' means "no colour of my own"), * placeholder, attrs (extra attributes for the HEX field, e.g. data-azp-color), compact. */ function azlinkpro_colour_picker($args) { $a = array_merge(array('id' => '', 'name' => '', 'value' => '', 'swatches' => azlinkpro_colour_swatches(), 'reset_label' => '', 'reset_value' => '', 'placeholder' => '#FFFFFF', 'attrs' => array(), 'compact' => false, 'describedby' => ''), $args); $value = azlinkpro_colour_hex($a['value']); $swatch_keys = array_map('strtoupper', array_keys($a['swatches'])); // The Custom box starts white, and shows the colour only when it is one of your own (not a Quick pick). $custom = ($value !== '' && !in_array($value, $swatch_keys, true)) ? $value : '#FFFFFF'; $attrs = ''; foreach ((array) $a['attrs'] as $k => $v) { $attrs .= ' ' . esc_attr($k) . '="' . esc_attr($v) . '"'; } echo '