'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 '
'; echo '
'; echo ''; echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $attrs is built only from esc_attr() keys and values above. if ($a['reset_label'] !== '') { echo ''; } echo '
'; if ($a['swatches']) { echo '
Quick picks:'; foreach ($a['swatches'] as $hex => $label) { $hex = azlinkpro_colour_hex($hex); echo ''; } echo '
'; } echo '
'; } /** Loads the picker's script and styles on the AzlinkPRO editors and the AI Review Builder page. */ function azlinkpro_colour_picker_assets($hook) { $screen = function_exists('get_current_screen') ? get_current_screen() : null; $editor = in_array($hook, array('post.php', 'post-new.php'), true) && $screen && in_array($screen->post_type, array('azlinkpro_table', 'azlinkpro_showcase', 'azlinkpro_proscons', 'azlinkpro_image'), true); $ai_page = $screen && strpos((string) $screen->id, 'azlinkpro-ai-builder') !== false; if (!$editor && !$ai_page) { return; } $main = AZLINKPRO_PATH . 'azlinkpro.php'; wp_enqueue_style('azlinkpro-colour-picker', plugins_url('assets/colour-picker.css', $main), array(), AZLINKPRO_VERSION); wp_enqueue_script('azlinkpro-colour-picker', plugins_url('assets/colour-picker.js', $main), array(), AZLINKPRO_VERSION, true); } add_action('admin_enqueue_scripts', 'azlinkpro_colour_picker_assets');