3 * Attaches the behaviors for the Color module.
6 (function ($, Drupal) {
8 * Displays farbtastic color selector and initialize color administration UI.
10 * @type {Drupal~behavior}
12 * @prop {Drupal~behaviorAttach} attach
13 * Attach color selection behavior to relevant context.
15 Drupal.behaviors.color = {
16 attach(context, settings) {
20 // This behavior attaches by ID, so is only valid once on a page.
21 const form = $(context).find('#system-theme-settings .color-form').once('color');
22 if (form.length === 0) {
31 $('<div class="color-placeholder"></div>').once('color').prependTo(form);
32 const farb = $.farbtastic('.color-placeholder');
34 // Decode reference colors to HSL.
35 const reference = settings.color.reference;
36 Object.keys(reference || {}).forEach((color) => {
37 reference[color] = farb.RGBToHSL(farb.unpack(reference[color]));
43 // Loop through all defined gradients.
44 Object.keys(settings.gradients || {}).forEach((i) => {
45 // Add element to display the gradient.
46 $('.color-preview').once('color').append(`<div id="gradient-${i}"></div>`);
47 const gradient = $(`.color-preview #gradient-${i}`);
48 // Add height of current gradient to the list (divided by 10).
49 height.push(parseInt(gradient.css('height'), 10) / 10);
50 // Add width of current gradient to the list (divided by 10).
51 width.push(parseInt(gradient.css('width'), 10) / 10);
52 // Add rows (or columns for horizontal gradients).
53 // Each gradient line should have a height (or width for horizontal
54 // gradients) of 10px (because we divided the height/width by 10
56 for (j = 0; j < (settings.gradients[i].direction === 'vertical' ? height[i] : width[i]); ++j) {
57 gradient.append('<div class="gradient-line"></div>');
61 // Set up colorScheme selector.
62 form.find('#edit-scheme').on('change', function () {
63 const schemes = settings.color.schemes;
64 const colorScheme = this.options[this.selectedIndex].value;
65 if (colorScheme !== '' && schemes[colorScheme]) {
66 // Get colors of active scheme.
67 colors = schemes[colorScheme];
68 Object.keys(colors || {}).forEach((fieldName) => {
69 callback($(`#edit-palette-${fieldName}`), colors[fieldName], false, true);
76 * Renders the preview.
79 Drupal.color.callback(context, settings, form, farb, height, width);
83 * Shifts a given color, using a reference pair (ref in HSL).
85 * This algorithm ensures relative ordering on the saturation and
86 * luminance axes is preserved, and performs a simple hue shift.
88 * It is also symmetrical. If: shiftColor(c, a, b) === d, then
89 * shiftColor(d, b, a) === c.
91 * @function Drupal.color~shiftColor
93 * @param {string} given
94 * A hex color code to shift.
95 * @param {Array.<number>} ref1
96 * First HSL color reference.
97 * @param {Array.<number>} ref2
98 * Second HSL color reference.
101 * A hex color, shifted.
103 function shiftColor(given, ref1, ref2) {
106 given = farb.RGBToHSL(farb.unpack(given));
109 given[0] += ref2[0] - ref1[0];
111 // Saturation: interpolate.
112 if (ref1[1] === 0 || ref2[1] === 0) {
116 d = ref1[1] / ref2[1];
121 given[1] = 1 - ((1 - given[1]) * d);
125 // Luminance: interpolate.
126 if (ref1[2] === 0 || ref2[2] === 0) {
130 d = ref1[2] / ref2[2];
135 given[2] = 1 - ((1 - given[2]) * d);
139 return farb.pack(farb.HSLToRGB(given));
143 * Callback for Farbtastic when a new color is chosen.
145 * @param {HTMLElement} input
146 * The input element where the color is chosen.
147 * @param {string} color
148 * The color that was chosen through the input.
149 * @param {bool} propagate
150 * Whether or not to propagate the color to a locked pair value
151 * @param {bool} colorScheme
152 * Flag to indicate if the user is using a color scheme when changing
155 function callback(input, color, propagate, colorScheme) {
157 // Set background/foreground colors.
159 backgroundColor: color,
160 color: farb.RGBToHSL(farb.unpack(color))[2] > 0.5 ? '#000' : '#fff',
163 // Change input value.
164 if ($(input).val() && $(input).val() !== color) {
167 // Update locked values.
170 for (j = i + 1; ; ++j) {
171 if (!locks[j - 1] || $(locks[j - 1]).is('.is-unlocked')) {
174 matched = shiftColor(color, reference[input.key], reference[inputs[j].key]);
175 callback(inputs[j], matched, false);
177 for (j = i - 1; ; --j) {
178 if (!locks[j] || $(locks[j]).is('.is-unlocked')) {
181 matched = shiftColor(color, reference[input.key], reference[inputs[j].key]);
182 callback(inputs[j], matched, false);
189 // Reset colorScheme selector.
197 * Resets the color scheme selector.
199 function resetScheme() {
200 form.find('#edit-scheme').each(function () {
201 this.selectedIndex = this.options.length - 1;
206 * Focuses Farbtastic on a particular field.
208 * @param {jQuery.Event} e
209 * The focus event on the field.
212 const input = e.target;
213 // Remove old bindings.
216 .off('keyup', farb.updateValue)
217 .off('keyup', preview)
218 .off('keyup', resetScheme)
220 .removeClass('item-selected');
225 farb.linkTo((color) => {
226 callback(input, color, true, false);
228 farb.setColor(input.value);
230 .on('keyup', farb.updateValue)
231 .on('keyup', preview)
232 .on('keyup', resetScheme)
234 .addClass('item-selected');
237 // Initialize color fields.
238 form.find('.js-color-palette input.form-text')
240 // Extract palette field name.
241 this.key = this.id.substring(13);
243 // Link to color picker temporarily to initialize.
244 farb.linkTo(() => {}).setColor('#000').linkTo(this);
247 const i = inputs.length;
249 let toggleClick = true;
250 const lock = $(`<button class="color-palette__lock">${Drupal.t('Unlock')}</button>`).on('click', function (e) {
253 $(this).addClass('is-unlocked').html(Drupal.t('Lock'));
254 $(hooks[i - 1]).attr('class',
255 locks[i - 2] && $(locks[i - 2]).is(':not(.is-unlocked)') ? 'color-palette__hook is-up' : 'color-palette__hook',
257 $(hooks[i]).attr('class',
258 locks[i] && $(locks[i]).is(':not(.is-unlocked)') ? 'color-palette__hook is-down' : 'color-palette__hook',
262 $(this).removeClass('is-unlocked').html(Drupal.t('Unlock'));
263 $(hooks[i - 1]).attr('class',
264 locks[i - 2] && $(locks[i - 2]).is(':not(.is-unlocked)') ? 'color-palette__hook is-both' : 'color-palette__hook is-down',
266 $(hooks[i]).attr('class',
267 locks[i] && $(locks[i]).is(':not(.is-unlocked)') ? 'color-palette__hook is-both' : 'color-palette__hook is-up',
270 toggleClick = !toggleClick;
277 const hook = $('<div class="color-palette__hook"></div>');
281 $(this).parent().find('.color-palette__lock').trigger('click');
287 form.find('.js-color-palette label');
289 // Focus first color.