Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(445)

Side by Side Diff: Source/devtools/front_end/elements/Spectrum.js

Issue 1297903005: DevTools: improve the color picker palette sorting algorithm. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Brian Grinstead All rights reserved. 2 * Copyright (C) 2011 Brian Grinstead All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 * @param {boolean} animate 234 * @param {boolean} animate
235 * @param {!Event=} event 235 * @param {!Event=} event
236 */ 236 */
237 _showPalette: function(palette, animate, event) 237 _showPalette: function(palette, animate, event)
238 { 238 {
239 this._paletteContainer.removeChildren(); 239 this._paletteContainer.removeChildren();
240 for (var i = 0; i < palette.colors.length; i++) { 240 for (var i = 0; i < palette.colors.length; i++) {
241 var animationDelay = animate ? i * 100 / palette.colors.length : 0; 241 var animationDelay = animate ? i * 100 / palette.colors.length : 0;
242 var colorElement = this._createPaletteColor(palette.colors[i], anima tionDelay); 242 var colorElement = this._createPaletteColor(palette.colors[i], anima tionDelay);
243 colorElement.addEventListener("click", this._paletteColorSelected.bi nd(this, palette.colors[i])); 243 colorElement.addEventListener("click", this._paletteColorSelected.bi nd(this, palette.colors[i]));
244 colorElement.addEventListener("mouseover", this._liveApplyStart.bind (this, palette.colors[i]));
245 colorElement.addEventListener("mouseout", this._liveApplyEnd.bind(th is));
246 if (palette.mutable) 244 if (palette.mutable)
247 colorElement.addEventListener("contextmenu", this._showPaletteCo lorContextMenu.bind(this, i)); 245 colorElement.addEventListener("contextmenu", this._showPaletteCo lorContextMenu.bind(this, i));
248 this._paletteContainer.appendChild(colorElement); 246 this._paletteContainer.appendChild(colorElement);
249 } 247 }
250 248
251 var numItems = palette.colors.length; 249 var numItems = palette.colors.length;
252 if (palette.mutable) 250 if (palette.mutable)
253 numItems++; 251 numItems++;
254 var rowsNeeded = Math.max(1, Math.ceil(numItems / 8)); 252 var rowsNeeded = Math.max(1, Math.ceil(numItems / 8));
255 for (var i = 0; palette.mutable && i < rowsNeeded * 8 - numItems; i++) 253 for (var i = 0; palette.mutable && i < rowsNeeded * 8 - numItems; i++)
256 this._paletteContainer.createChild("div", "spectrum-palette-color em pty-color"); 254 this._paletteContainer.createChild("div", "spectrum-palette-color em pty-color");
257 if (palette.mutable) 255 if (palette.mutable)
258 this._paletteContainer.appendChild(this._addColorToolbar.element); 256 this._paletteContainer.appendChild(this._addColorToolbar.element);
259 257
260 this._togglePalettePanel(false); 258 this._togglePalettePanel(false);
261 var paletteColorHeight = 12; 259 var paletteColorHeight = 12;
262 var paletteMargin = 12; 260 var paletteMargin = 12;
263 this.element.style.height = (this._paletteContainer.offsetTop + paletteM argin + (paletteColorHeight + paletteMargin) * rowsNeeded) + "px"; 261 this.element.style.height = (this._paletteContainer.offsetTop + paletteM argin + (paletteColorHeight + paletteMargin) * rowsNeeded) + "px";
264 this.dispatchEventToListeners(WebInspector.Spectrum.Events.SizeChanged); 262 this.dispatchEventToListeners(WebInspector.Spectrum.Events.SizeChanged);
265 }, 263 },
266 264
267 /** 265 /**
268 * @param {string} colorText
269 */
270 _liveApplyStart: function(colorText)
271 {
272 this._underlyingHSV = this._hsv;
273 this._underlyingFormat = this._colorFormat;
274 this._underlyingColorString = this._colorString;
275 var color = WebInspector.Color.parse(colorText);
276 if (!color)
277 return;
278 this._innerSetColor(color.hsva(), colorText, color.format(), WebInspecto r.Spectrum._ChangeSource.Other);
279 },
280
281 _liveApplyEnd: function()
282 {
283 if (!this._underlyingHSV)
284 return;
285 this._innerSetColor(this._underlyingHSV, this._underlyingColorString, th is._underlyingFormat, WebInspector.Spectrum._ChangeSource.Other);
286 delete this._underlyingHSV;
287 delete this._underlyingFormat;
288 delete this._underlyingColorString;
289 },
290
291 /**
292 * @param {!WebInspector.Spectrum.Palette} generatedPalette 266 * @param {!WebInspector.Spectrum.Palette} generatedPalette
293 */ 267 */
294 _generatedPaletteLoaded: function(generatedPalette) 268 _generatedPaletteLoaded: function(generatedPalette)
295 { 269 {
296 if (generatedPalette.colors.length) 270 if (generatedPalette.colors.length)
297 this._palettes.set(generatedPalette.title, generatedPalette); 271 this._palettes.set(generatedPalette.title, generatedPalette);
298 this._palettes.set(WebInspector.Spectrum.MaterialPalette.title, WebInspe ctor.Spectrum.MaterialPalette); 272 this._palettes.set(WebInspector.Spectrum.MaterialPalette.title, WebInspe ctor.Spectrum.MaterialPalette);
299 /** @type {!WebInspector.Spectrum.Palette} */ 273 /** @type {!WebInspector.Spectrum.Palette} */
300 var defaultCustomPalette = { title: "Custom", colors: [], mutable: true }; 274 var defaultCustomPalette = { title: "Custom", colors: [], mutable: true };
301 this._customPaletteSetting = WebInspector.settings.createSetting("custom ColorPalette", defaultCustomPalette); 275 this._customPaletteSetting = WebInspector.settings.createSetting("custom ColorPalette", defaultCustomPalette);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 }, 310 },
337 311
338 /** 312 /**
339 * @param {string} colorText 313 * @param {string} colorText
340 */ 314 */
341 _paletteColorSelected: function(colorText) 315 _paletteColorSelected: function(colorText)
342 { 316 {
343 var color = WebInspector.Color.parse(colorText); 317 var color = WebInspector.Color.parse(colorText);
344 if (!color) 318 if (!color)
345 return; 319 return;
346 this._innerSetColor(color.hsva(), colorText, color.format(), WebInspecto r.Spectrum._ChangeSource.Other); 320 this._innerSetColor(color.hsva(), colorText, undefined, WebInspector.Spe ctrum._ChangeSource.Other);
347 delete this._underlyingHSV;
348 }, 321 },
349 322
350 _addColorToCustomPalette: function() 323 _addColorToCustomPalette: function()
351 { 324 {
352 var palette = this._customPaletteSetting.get(); 325 var palette = this._customPaletteSetting.get();
353 palette.colors.push(this.colorString()); 326 palette.colors.push(this.colorString());
354 this._customPaletteSetting.set(palette); 327 this._customPaletteSetting.set(palette);
355 this._showPalette(this._customPaletteSetting.get(), false); 328 this._showPalette(this._customPaletteSetting.get(), false);
356 }, 329 },
357 330
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 727
755 _finish: function() 728 _finish: function()
756 { 729 {
757 /** 730 /**
758 * @param {string} a 731 * @param {string} a
759 * @param {string} b 732 * @param {string} b
760 * @return {number} 733 * @return {number}
761 */ 734 */
762 function hueComparator(a, b) 735 function hueComparator(a, b)
763 { 736 {
764 return paletteColors.get(b).hsva()[0] - paletteColors.get(a).hsva()[ 0]; 737 var hsva = paletteColors.get(a).hsva();
738 var hsvb = paletteColors.get(b).hsva();
739
740 // First trim the shades of gray
741 if (hsvb[1] < 0.12 && hsva[1] < 0.12)
742 return hsvb[2]*hsvb[3] - hsva[2]*hsva[3];
743 if (hsvb[1] < 0.12)
744 return -1;
745 if (hsva[1] < 0.12)
746 return 1;
747
748 // Equal hue -> sort by sat
749 if (hsvb[0] === hsva[0])
750 return hsvb[1]*hsvb[3] - hsva[1]*hsva[3];
dgozman 2015/08/19 20:27:44 nit: spaces around *
751
752 return (hsvb[0] + 0.94) % 1 - (hsva[0] + 0.94) % 1;
765 } 753 }
766 754
767 var colors = this._frequencyMap.keysArray(); 755 var colors = this._frequencyMap.keysArray();
768 colors = colors.sort(this._frequencyComparator.bind(this)); 756 colors = colors.sort(this._frequencyComparator.bind(this));
769 /** @type {!Map.<string, !WebInspector.Color>} */ 757 /** @type {!Map.<string, !WebInspector.Color>} */
770 var paletteColors = new Map(); 758 var paletteColors = new Map();
771 var colorsPerRow = 24; 759 var colorsPerRow = 24;
772 while (paletteColors.size < colorsPerRow && colors.length) { 760 while (paletteColors.size < colorsPerRow && colors.length) {
773 var colorText = colors.shift(); 761 var colorText = colors.shift();
774 var color = WebInspector.Color.parse(colorText); 762 var color = WebInspector.Color.parse(colorText);
(...skipping 11 matching lines...) Expand all
786 * @this {WebInspector.Spectrum.PaletteGenerator} 774 * @this {WebInspector.Spectrum.PaletteGenerator}
787 */ 775 */
788 _processStylesheet: function(stylesheet, resolve) 776 _processStylesheet: function(stylesheet, resolve)
789 { 777 {
790 /** 778 /**
791 * @param {?string} text 779 * @param {?string} text
792 * @this {WebInspector.Spectrum.PaletteGenerator} 780 * @this {WebInspector.Spectrum.PaletteGenerator}
793 */ 781 */
794 function parseContent(text) 782 function parseContent(text)
795 { 783 {
796 var regexResult = text.match(/((?:rgb|hsl)a?\([^)]+\)|#[0-9a-fA-F]{6 }|#[0-9a-fA-F]{3})/g) || []; 784 text = text.toLowerCase();
785 var regexResult = text.match(/((?:rgb|hsl)a?\([^)]+\)|#[0-9a-f]{6}|# [0-9a-f]{3})/g) || [];
797 for (var c of regexResult) { 786 for (var c of regexResult) {
798 var frequency = this._frequencyMap.get(c) || 0; 787 var frequency = this._frequencyMap.get(c) || 0;
799 this._frequencyMap.set(c, ++frequency); 788 this._frequencyMap.set(c, ++frequency);
800 } 789 }
801 resolve(null); 790 resolve(null);
802 } 791 }
803 792
804 stylesheet.requestContent(parseContent.bind(this)); 793 stylesheet.requestContent(parseContent.bind(this));
805 } 794 }
806 } 795 }
(...skipping 12 matching lines...) Expand all
819 "#8BC34A", 808 "#8BC34A",
820 "#CDDC39", 809 "#CDDC39",
821 "#FFEB3B", 810 "#FFEB3B",
822 "#FFC107", 811 "#FFC107",
823 "#FF9800", 812 "#FF9800",
824 "#FF5722", 813 "#FF5722",
825 "#795548", 814 "#795548",
826 "#9E9E9E", 815 "#9E9E9E",
827 "#607D8B" 816 "#607D8B"
828 ]}; 817 ]};
OLDNEW
« no previous file with comments | « no previous file | Source/devtools/front_end/elements/spectrum.css » ('j') | Source/devtools/front_end/elements/spectrum.css » ('J')

Powered by Google App Engine
This is Rietveld 408576698