3 * Attaches the behaviors for the Color module.
6 (function ($, Drupal) {
11 * Displays farbtastic color selector and initialize color administration UI.
13 * @type {Drupal~behavior}
15 * @prop {Drupal~behaviorAttach} attach
16 * Attach color selection behavior to relevant context.
18 Drupal.behaviors.color = {
19 attach: function (context, settings) {
23 // This behavior attaches by ID, so is only valid once on a page.
24 var form = $(context).find('#system-theme-settings .color-form').once('color');
25 if (form.length === 0) {
34 $('<div class="color-placeholder"></div>').once('color').prependTo(form);
35 var farb = $.farbtastic('.color-placeholder');
37 // Decode reference colors to HSL.
38 var reference = settings.color.reference;
39 for (i in reference) {
40 if (reference.hasOwnProperty(i)) {
41 reference[i] = farb.RGBToHSL(farb.unpack(reference[i]));
48 // Loop through all defined gradients.
49 for (i in settings.gradients) {
50 if (settings.gradients.hasOwnProperty(i)) {
51 // Add element to display the gradient.
52 $('.color-preview').once('color').append('<div id="gradient-' + i + '"></div>');
53 var gradient = $('.color-preview #gradient-' + i);
54 // Add height of current gradient to the list (divided by 10).
55 height.push(parseInt(gradient.css('height'), 10) / 10);
56 // Add width of current gradient to the list (divided by 10).
57 width.push(parseInt(gradient.css('width'), 10) / 10);
58 // Add rows (or columns for horizontal gradients).
59 // Each gradient line should have a height (or width for horizontal
60 // gradients) of 10px (because we divided the height/width by 10
62 for (j = 0; j < (settings.gradients[i].direction === 'vertical' ? height[i] : width[i]); ++j) {
63 gradient.append('<div class="gradient-line"></div>');
68 // Set up colorScheme selector.
69 form.find('#edit-scheme').on('change', function () {
70 var schemes = settings.color.schemes;
71 var colorScheme = this.options[this.selectedIndex].value;
72 if (colorScheme !== '' && schemes[colorScheme]) {
73 // Get colors of active scheme.
74 colors = schemes[colorScheme];
75 for (var fieldName in colors) {
76 if (colors.hasOwnProperty(fieldName)) {
77 callback($('#edit-palette-' + fieldName), colors[fieldName], false, true);
85 * Renders the preview.
88 Drupal.color.callback(context, settings, form, farb, height, width);
92 * Shifts a given color, using a reference pair (ref in HSL).
94 * This algorithm ensures relative ordering on the saturation and
95 * luminance axes is preserved, and performs a simple hue shift.
97 * It is also symmetrical. If: shift_color(c, a, b) === d, then
98 * shift_color(d, b, a) === c.
100 * @function Drupal.color~shift_color
102 * @param {string} given
103 * A hex color code to shift.
104 * @param {Array.<number>} ref1
105 * First HSL color reference.
106 * @param {Array.<number>} ref2
107 * Second HSL color reference.
110 * A hex color, shifted.
112 function shift_color(given, ref1, ref2) {
115 given = farb.RGBToHSL(farb.unpack(given));
118 given[0] += ref2[0] - ref1[0];
120 // Saturation: interpolate.
121 if (ref1[1] === 0 || ref2[1] === 0) {
125 d = ref1[1] / ref2[1];
130 given[1] = 1 - (1 - given[1]) * d;
134 // Luminance: interpolate.
135 if (ref1[2] === 0 || ref2[2] === 0) {
139 d = ref1[2] / ref2[2];
144 given[2] = 1 - (1 - given[2]) * d;
148 return farb.pack(farb.HSLToRGB(given));
152 * Callback for Farbtastic when a new color is chosen.
154 * @param {HTMLElement} input
155 * The input element where the color is chosen.
156 * @param {string} color
157 * The color that was chosen through the input.
158 * @param {bool} propagate
159 * Whether or not to propagate the color to a locked pair value
160 * @param {bool} colorScheme
161 * Flag to indicate if the user is using a color scheme when changing
164 function callback(input, color, propagate, colorScheme) {
166 // Set background/foreground colors.
168 backgroundColor: color,
169 color: farb.RGBToHSL(farb.unpack(color))[2] > 0.5 ? '#000' : '#fff'
172 // Change input value.
173 if ($(input).val() && $(input).val() !== color) {
176 // Update locked values.
179 for (j = i + 1; ; ++j) {
180 if (!locks[j - 1] || $(locks[j - 1]).is('.is-unlocked')) {
183 matched = shift_color(color, reference[input.key], reference[inputs[j].key]);
184 callback(inputs[j], matched, false);
186 for (j = i - 1; ; --j) {
187 if (!locks[j] || $(locks[j]).is('.is-unlocked')) {
190 matched = shift_color(color, reference[input.key], reference[inputs[j].key]);
191 callback(inputs[j], matched, false);
198 // Reset colorScheme selector.
206 * Resets the color scheme selector.
208 function resetScheme() {
209 form.find('#edit-scheme').each(function () {
210 this.selectedIndex = this.options.length - 1;
215 * Focuses Farbtastic on a particular field.
217 * @param {jQuery.Event} e
218 * The focus event on the field.
221 var input = e.target;
222 // Remove old bindings.
224 $(focused).off('keyup', farb.updateValue)
225 .off('keyup', preview).off('keyup', resetScheme)
226 .parent().removeClass('item-selected');
231 farb.linkTo(function (color) { callback(input, color, true, false); });
232 farb.setColor(input.value);
233 $(focused).on('keyup', farb.updateValue).on('keyup', preview).on('keyup', resetScheme)
234 .parent().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(function () {}).setColor('#000').linkTo(this);
247 var i = inputs.length;
249 var toggleClick = true;
250 var 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 var 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.