Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Joseph Pecoraro | 3 * Copyright (C) 2009 Joseph Pecoraro |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 } | 55 } |
| 56 | 56 |
| 57 /** | 57 /** |
| 58 * @enum {string} | 58 * @enum {string} |
| 59 */ | 59 */ |
| 60 WebInspector.Color.Format = { | 60 WebInspector.Color.Format = { |
| 61 Original: "original", | 61 Original: "original", |
| 62 Nickname: "nickname", | 62 Nickname: "nickname", |
| 63 HEX: "hex", | 63 HEX: "hex", |
| 64 ShortHEX: "shorthex", | 64 ShortHEX: "shorthex", |
| 65 HEXA: "hexa", | |
| 66 ShortHEXA: "shorthexa", | |
| 65 RGB: "rgb", | 67 RGB: "rgb", |
| 66 RGBA: "rgba", | 68 RGBA: "rgba", |
| 67 HSL: "hsl", | 69 HSL: "hsl", |
| 68 HSLA: "hsla" | 70 HSLA: "hsla" |
| 69 } | 71 } |
| 70 | 72 |
| 71 /** | 73 /** |
| 72 * @param {string} text | 74 * @param {string} text |
| 73 * @return {?WebInspector.Color} | 75 * @return {?WebInspector.Color} |
| 74 */ | 76 */ |
| 75 WebInspector.Color.parse = function(text) | 77 WebInspector.Color.parse = function(text) |
| 76 { | 78 { |
| 77 // Simple - #hex, rgb(), nickname, hsl() | 79 // Simple - #hex, rgb(), nickname, hsl() |
| 78 var value = text.toLowerCase().replace(/\s+/g, ""); | 80 var value = text.toLowerCase().replace(/\s+/g, ""); |
| 79 var simple = /^(?:#([0-9a-f]{3}|[0-9a-f]{6})|rgb\(((?:-?\d+%?,){2}-?\d+%?)\) |(\w+)|hsl\((-?\d+\.?\d*(?:,-?\d+\.?\d*%){2})\))$/i; | 81 var simple = /^(?:#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})|rgb\(((?:-?\d+%?, ){2}-?\d+%?)\)|(\w+)|hsl\((-?\d+\.?\d*(?:,-?\d+\.?\d*%){2})\))$/i; |
| 80 var match = value.match(simple); | 82 var match = value.match(simple); |
| 81 if (match) { | 83 if (match) { |
| 82 if (match[1]) { // hex | 84 if (match[1]) { // hex |
| 83 var hex = match[1].toLowerCase(); | 85 var hex = match[1].toLowerCase(); |
| 84 var format; | 86 var format; |
| 85 if (hex.length === 3) { | 87 if (hex.length === 3) { |
| 86 format = WebInspector.Color.Format.ShortHEX; | 88 format = WebInspector.Color.Format.ShortHEX; |
| 87 hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt (1) + hex.charAt(2) + hex.charAt(2); | 89 hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt (1) + hex.charAt(2) + hex.charAt(2); |
| 88 } else | 90 } else if (hex.length === 4) { |
| 91 format = WebInspector.Color.Format.ShortHEXA; | |
| 92 hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt (1) + hex.charAt(2) + hex.charAt(2) + hex.charAt(3) + hex.charAt(3); | |
| 93 } else if (hex.length === 6) { | |
| 89 format = WebInspector.Color.Format.HEX; | 94 format = WebInspector.Color.Format.HEX; |
| 95 } else { | |
| 96 format = WebInspector.Color.Format.HEXA; | |
| 97 } | |
| 90 var r = parseInt(hex.substring(0,2), 16); | 98 var r = parseInt(hex.substring(0,2), 16); |
| 91 var g = parseInt(hex.substring(2,4), 16); | 99 var g = parseInt(hex.substring(2,4), 16); |
| 92 var b = parseInt(hex.substring(4,6), 16); | 100 var b = parseInt(hex.substring(4,6), 16); |
| 93 return new WebInspector.Color([r / 255, g / 255, b / 255, 1], format , text); | 101 var a = hex.length === 8 ? parseInt(hex.substring(6, 8), 16) / 255 : 1; |
| 102 return new WebInspector.Color([r / 255, g / 255, b / 255, a], format , text); | |
| 94 } | 103 } |
| 95 | 104 |
| 96 if (match[2]) { // rgb | 105 if (match[2]) { // rgb |
| 97 var rgbString = match[2].split(/\s*,\s*/); | 106 var rgbString = match[2].split(/\s*,\s*/); |
| 98 var rgba = [ WebInspector.Color._parseRgbNumeric(rgbString[0]), | 107 var rgba = [ WebInspector.Color._parseRgbNumeric(rgbString[0]), |
| 99 WebInspector.Color._parseRgbNumeric(rgbString[1]), | 108 WebInspector.Color._parseRgbNumeric(rgbString[1]), |
| 100 WebInspector.Color._parseRgbNumeric(rgbString[2]), 1 ]; | 109 WebInspector.Color._parseRgbNumeric(rgbString[2]), 1 ]; |
| 101 return new WebInspector.Color(rgba, WebInspector.Color.Format.RGB, t ext); | 110 return new WebInspector.Color(rgba, WebInspector.Color.Format.RGB, t ext); |
| 102 } | 111 } |
| 103 | 112 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 | 256 |
| 248 /** | 257 /** |
| 249 * @return {boolean} | 258 * @return {boolean} |
| 250 */ | 259 */ |
| 251 hasAlpha: function() | 260 hasAlpha: function() |
| 252 { | 261 { |
| 253 return this._rgba[3] !== 1; | 262 return this._rgba[3] !== 1; |
| 254 }, | 263 }, |
| 255 | 264 |
| 256 /** | 265 /** |
| 257 * @return {boolean} | 266 * @return {!WebInspector.Color.Format} |
| 258 */ | 267 */ |
| 259 canBeShortHex: function() | 268 detectHEXFormat: function() |
| 260 { | 269 { |
| 261 if (this.hasAlpha()) | 270 var canBeShort = true; |
| 262 return false; | 271 for (var i = 0; i < 4; ++i) { |
| 263 for (var i = 0; i < 3; ++i) { | |
| 264 var c = Math.round(this._rgba[i] * 255); | 272 var c = Math.round(this._rgba[i] * 255); |
| 265 if (c % 17) | 273 if (c % 17) |
| 266 return false; | 274 canBeShort = false; |
| 267 } | 275 } |
| 268 return true; | 276 var hasAlpha = this.hasAlpha(); |
| 277 var cf = WebInspector.Color.Format; | |
| 278 if (canBeShort) | |
| 279 return hasAlpha ? cf.ShortHEXA : cf.ShortHEX; | |
| 280 return hasAlpha ? cf.HEXA : cf.HEX; | |
| 269 }, | 281 }, |
| 270 | 282 |
| 271 /** | 283 /** |
| 272 * @return {?string} | 284 * @return {?string} |
| 273 */ | 285 */ |
| 274 asString: function(format) | 286 asString: function(format) |
| 275 { | 287 { |
| 276 if (format === this._format && this._originalTextIsValid) | 288 if (format === this._format && this._originalTextIsValid) |
| 277 return this._originalText; | 289 return this._originalText; |
| 278 | 290 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 case WebInspector.Color.Format.RGBA: | 329 case WebInspector.Color.Format.RGBA: |
| 318 return String.sprintf("rgba(%d, %d, %d, %f)", toRgbValue(this._rgba[ 0]), toRgbValue(this._rgba[1]), toRgbValue(this._rgba[2]), this._rgba[3]); | 330 return String.sprintf("rgba(%d, %d, %d, %f)", toRgbValue(this._rgba[ 0]), toRgbValue(this._rgba[1]), toRgbValue(this._rgba[2]), this._rgba[3]); |
| 319 case WebInspector.Color.Format.HSL: | 331 case WebInspector.Color.Format.HSL: |
| 320 if (this.hasAlpha()) | 332 if (this.hasAlpha()) |
| 321 return null; | 333 return null; |
| 322 var hsl = this.hsla(); | 334 var hsl = this.hsla(); |
| 323 return String.sprintf("hsl(%d, %d%, %d%)", Math.round(hsl[0] * 360), Math.round(hsl[1] * 100), Math.round(hsl[2] * 100)); | 335 return String.sprintf("hsl(%d, %d%, %d%)", Math.round(hsl[0] * 360), Math.round(hsl[1] * 100), Math.round(hsl[2] * 100)); |
| 324 case WebInspector.Color.Format.HSLA: | 336 case WebInspector.Color.Format.HSLA: |
| 325 var hsla = this.hsla(); | 337 var hsla = this.hsla(); |
| 326 return String.sprintf("hsla(%d, %d%, %d%, %f)", Math.round(hsla[0] * 360), Math.round(hsla[1] * 100), Math.round(hsla[2] * 100), hsla[3]); | 338 return String.sprintf("hsla(%d, %d%, %d%, %f)", Math.round(hsla[0] * 360), Math.round(hsla[1] * 100), Math.round(hsla[2] * 100), hsla[3]); |
| 339 case WebInspector.Color.Format.HEXA: | |
| 340 return String.sprintf("#%s%s%s%s", toHexValue(this._rgba[0]), toHexV alue(this._rgba[1]), toHexValue(this._rgba[2]), toHexValue(this._rgba[3])).toLow erCase(); | |
| 327 case WebInspector.Color.Format.HEX: | 341 case WebInspector.Color.Format.HEX: |
| 328 if (this.hasAlpha()) | 342 if (this.hasAlpha()) |
| 329 return null; | 343 return null; |
| 330 return String.sprintf("#%s%s%s", toHexValue(this._rgba[0]), toHexVal ue(this._rgba[1]), toHexValue(this._rgba[2])).toLowerCase();; | 344 return String.sprintf("#%s%s%s", toHexValue(this._rgba[0]), toHexVal ue(this._rgba[1]), toHexValue(this._rgba[2])).toLowerCase(); |
| 345 case WebInspector.Color.Format.ShortHEXA: | |
| 346 if (this.detectHEXFormat() !== WebInspector.Color.Format.ShortHEXA & & this.detectHEXFormat() !== WebInspector.Color.Format.ShortHEX) | |
|
dgozman
2016/05/20 00:55:15
Let's call detectHEXFormat once.
samli
2016/05/20 01:58:02
Done
| |
| 347 return null; | |
| 348 return String.sprintf("#%s%s%s%s", toShortHexValue(this._rgba[0]), t oShortHexValue(this._rgba[1]), toShortHexValue(this._rgba[2]), toShortHexValue(t his._rgba[3])).toLowerCase(); | |
| 331 case WebInspector.Color.Format.ShortHEX: | 349 case WebInspector.Color.Format.ShortHEX: |
| 332 if (!this.canBeShortHex()) | 350 if (this.detectHEXFormat() !== WebInspector.Color.Format.ShortHEX) |
| 333 return null; | 351 return null; |
| 334 return String.sprintf("#%s%s%s", toShortHexValue(this._rgba[0]), toS hortHexValue(this._rgba[1]), toShortHexValue(this._rgba[2])).toLowerCase();; | 352 return String.sprintf("#%s%s%s", toShortHexValue(this._rgba[0]), toS hortHexValue(this._rgba[1]), toShortHexValue(this._rgba[2])).toLowerCase(); |
| 335 case WebInspector.Color.Format.Nickname: | 353 case WebInspector.Color.Format.Nickname: |
| 336 return this.nickname(); | 354 return this.nickname(); |
| 337 } | 355 } |
| 338 | 356 |
| 339 return this._originalText; | 357 return this._originalText; |
| 340 }, | 358 }, |
| 341 | 359 |
| 342 | 360 |
| 343 /** | 361 /** |
| 344 * @return {!Array<number>} | 362 * @return {!Array<number>} |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 806 { | 824 { |
| 807 const cf = WebInspector.Color.Format; | 825 const cf = WebInspector.Color.Format; |
| 808 var format; | 826 var format; |
| 809 var formatSetting = WebInspector.moduleSetting("colorFormat").get(); | 827 var formatSetting = WebInspector.moduleSetting("colorFormat").get(); |
| 810 if (formatSetting === cf.Original) | 828 if (formatSetting === cf.Original) |
| 811 format = cf.Original; | 829 format = cf.Original; |
| 812 else if (formatSetting === cf.RGB) | 830 else if (formatSetting === cf.RGB) |
| 813 format = (color.hasAlpha() ? cf.RGBA : cf.RGB); | 831 format = (color.hasAlpha() ? cf.RGBA : cf.RGB); |
| 814 else if (formatSetting === cf.HSL) | 832 else if (formatSetting === cf.HSL) |
| 815 format = (color.hasAlpha() ? cf.HSLA : cf.HSL); | 833 format = (color.hasAlpha() ? cf.HSLA : cf.HSL); |
| 816 else if (!color.hasAlpha()) | 834 else if (formatSetting === cf.HEX) |
| 817 format = (color.canBeShortHex() ? cf.ShortHEX : cf.HEX); | 835 format = color.detectHEXFormat(); |
| 818 else | 836 else |
| 819 format = cf.RGBA; | 837 format = cf.RGBA; |
| 820 | 838 |
| 821 return format; | 839 return format; |
| 822 } | 840 } |
| OLD | NEW |