| 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", | |
| 67 RGB: "rgb", | 65 RGB: "rgb", |
| 68 RGBA: "rgba", | 66 RGBA: "rgba", |
| 69 HSL: "hsl", | 67 HSL: "hsl", |
| 70 HSLA: "hsla" | 68 HSLA: "hsla" |
| 71 } | 69 } |
| 72 | 70 |
| 73 /** | 71 /** |
| 74 * @param {string} text | 72 * @param {string} text |
| 75 * @return {?WebInspector.Color} | 73 * @return {?WebInspector.Color} |
| 76 */ | 74 */ |
| 77 WebInspector.Color.parse = function(text) | 75 WebInspector.Color.parse = function(text) |
| 78 { | 76 { |
| 79 // Simple - #hex, rgb(), nickname, hsl() | 77 // Simple - #hex, rgb(), nickname, hsl() |
| 80 var value = text.toLowerCase().replace(/\s+/g, ""); | 78 var value = text.toLowerCase().replace(/\s+/g, ""); |
| 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; | 79 var simple = /^(?:#([0-9a-f]{3}|[0-9a-f]{6})|rgb\(((?:-?\d+%?,){2}-?\d+%?)\)
|(\w+)|hsl\((-?\d+\.?\d*(?:,-?\d+\.?\d*%){2})\))$/i; |
| 82 var match = value.match(simple); | 80 var match = value.match(simple); |
| 83 if (match) { | 81 if (match) { |
| 84 if (match[1]) { // hex | 82 if (match[1]) { // hex |
| 85 var hex = match[1].toLowerCase(); | 83 var hex = match[1].toLowerCase(); |
| 86 var format; | 84 var format; |
| 87 if (hex.length === 3) { | 85 if (hex.length === 3) { |
| 88 format = WebInspector.Color.Format.ShortHEX; | 86 format = WebInspector.Color.Format.ShortHEX; |
| 89 hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt
(1) + hex.charAt(2) + hex.charAt(2); | 87 hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt
(1) + hex.charAt(2) + hex.charAt(2); |
| 90 } else if (hex.length === 4) { | 88 } else |
| 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) { | |
| 94 format = WebInspector.Color.Format.HEX; | 89 format = WebInspector.Color.Format.HEX; |
| 95 } else { | |
| 96 format = WebInspector.Color.Format.HEXA; | |
| 97 } | |
| 98 var r = parseInt(hex.substring(0,2), 16); | 90 var r = parseInt(hex.substring(0,2), 16); |
| 99 var g = parseInt(hex.substring(2,4), 16); | 91 var g = parseInt(hex.substring(2,4), 16); |
| 100 var b = parseInt(hex.substring(4,6), 16); | 92 var b = parseInt(hex.substring(4,6), 16); |
| 101 var a = hex.length === 8 ? parseInt(hex.substring(6, 8), 16) / 255 :
1; | 93 return new WebInspector.Color([r / 255, g / 255, b / 255, 1], format
, text); |
| 102 return new WebInspector.Color([r / 255, g / 255, b / 255, a], format
, text); | |
| 103 } | 94 } |
| 104 | 95 |
| 105 if (match[2]) { // rgb | 96 if (match[2]) { // rgb |
| 106 var rgbString = match[2].split(/\s*,\s*/); | 97 var rgbString = match[2].split(/\s*,\s*/); |
| 107 var rgba = [ WebInspector.Color._parseRgbNumeric(rgbString[0]), | 98 var rgba = [ WebInspector.Color._parseRgbNumeric(rgbString[0]), |
| 108 WebInspector.Color._parseRgbNumeric(rgbString[1]), | 99 WebInspector.Color._parseRgbNumeric(rgbString[1]), |
| 109 WebInspector.Color._parseRgbNumeric(rgbString[2]), 1 ]; | 100 WebInspector.Color._parseRgbNumeric(rgbString[2]), 1 ]; |
| 110 return new WebInspector.Color(rgba, WebInspector.Color.Format.RGB, t
ext); | 101 return new WebInspector.Color(rgba, WebInspector.Color.Format.RGB, t
ext); |
| 111 } | 102 } |
| 112 | 103 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 | 247 |
| 257 /** | 248 /** |
| 258 * @return {boolean} | 249 * @return {boolean} |
| 259 */ | 250 */ |
| 260 hasAlpha: function() | 251 hasAlpha: function() |
| 261 { | 252 { |
| 262 return this._rgba[3] !== 1; | 253 return this._rgba[3] !== 1; |
| 263 }, | 254 }, |
| 264 | 255 |
| 265 /** | 256 /** |
| 266 * @return {!WebInspector.Color.Format} | 257 * @return {boolean} |
| 267 */ | 258 */ |
| 268 detectHEXFormat: function() | 259 canBeShortHex: function() |
| 269 { | 260 { |
| 270 var canBeShort = true; | 261 if (this.hasAlpha()) |
| 271 for (var i = 0; i < 4; ++i) { | 262 return false; |
| 263 for (var i = 0; i < 3; ++i) { |
| 272 var c = Math.round(this._rgba[i] * 255); | 264 var c = Math.round(this._rgba[i] * 255); |
| 273 if (c % 17) | 265 if (c % 17) |
| 274 canBeShort = false; | 266 return false; |
| 275 } | 267 } |
| 276 var hasAlpha = this.hasAlpha(); | 268 return true; |
| 277 var cf = WebInspector.Color.Format; | |
| 278 if (canBeShort) | |
| 279 return hasAlpha ? cf.ShortHEXA : cf.ShortHEX; | |
| 280 return hasAlpha ? cf.HEXA : cf.HEX; | |
| 281 }, | 269 }, |
| 282 | 270 |
| 283 /** | 271 /** |
| 284 * @return {?string} | 272 * @return {?string} |
| 285 */ | 273 */ |
| 286 asString: function(format) | 274 asString: function(format) |
| 287 { | 275 { |
| 288 if (format === this._format && this._originalTextIsValid) | 276 if (format === this._format && this._originalTextIsValid) |
| 289 return this._originalText; | 277 return this._originalText; |
| 290 | 278 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 case WebInspector.Color.Format.RGBA: | 317 case WebInspector.Color.Format.RGBA: |
| 330 return String.sprintf("rgba(%d, %d, %d, %f)", toRgbValue(this._rgba[
0]), toRgbValue(this._rgba[1]), toRgbValue(this._rgba[2]), this._rgba[3]); | 318 return String.sprintf("rgba(%d, %d, %d, %f)", toRgbValue(this._rgba[
0]), toRgbValue(this._rgba[1]), toRgbValue(this._rgba[2]), this._rgba[3]); |
| 331 case WebInspector.Color.Format.HSL: | 319 case WebInspector.Color.Format.HSL: |
| 332 if (this.hasAlpha()) | 320 if (this.hasAlpha()) |
| 333 return null; | 321 return null; |
| 334 var hsl = this.hsla(); | 322 var hsl = this.hsla(); |
| 335 return String.sprintf("hsl(%d, %d%, %d%)", Math.round(hsl[0] * 360),
Math.round(hsl[1] * 100), Math.round(hsl[2] * 100)); | 323 return String.sprintf("hsl(%d, %d%, %d%)", Math.round(hsl[0] * 360),
Math.round(hsl[1] * 100), Math.round(hsl[2] * 100)); |
| 336 case WebInspector.Color.Format.HSLA: | 324 case WebInspector.Color.Format.HSLA: |
| 337 var hsla = this.hsla(); | 325 var hsla = this.hsla(); |
| 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]); | 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]); |
| 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(); | |
| 341 case WebInspector.Color.Format.HEX: | 327 case WebInspector.Color.Format.HEX: |
| 342 if (this.hasAlpha()) | 328 if (this.hasAlpha()) |
| 343 return null; | 329 return null; |
| 344 return String.sprintf("#%s%s%s", toHexValue(this._rgba[0]), toHexVal
ue(this._rgba[1]), toHexValue(this._rgba[2])).toLowerCase(); | 330 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 var hexFormat = this.detectHEXFormat(); | |
| 347 if (hexFormat !== WebInspector.Color.Format.ShortHEXA && hexFormat !
== WebInspector.Color.Format.ShortHEX) | |
| 348 return null; | |
| 349 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(); | |
| 350 case WebInspector.Color.Format.ShortHEX: | 331 case WebInspector.Color.Format.ShortHEX: |
| 351 if (this.detectHEXFormat() !== WebInspector.Color.Format.ShortHEX) | 332 if (!this.canBeShortHex()) |
| 352 return null; | 333 return null; |
| 353 return String.sprintf("#%s%s%s", toShortHexValue(this._rgba[0]), toS
hortHexValue(this._rgba[1]), toShortHexValue(this._rgba[2])).toLowerCase(); | 334 return String.sprintf("#%s%s%s", toShortHexValue(this._rgba[0]), toS
hortHexValue(this._rgba[1]), toShortHexValue(this._rgba[2])).toLowerCase(); |
| 354 case WebInspector.Color.Format.Nickname: | 335 case WebInspector.Color.Format.Nickname: |
| 355 return this.nickname(); | 336 return this.nickname(); |
| 356 } | 337 } |
| 357 | 338 |
| 358 return this._originalText; | 339 return this._originalText; |
| 359 }, | 340 }, |
| 360 | 341 |
| 361 | 342 |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 { | 806 { |
| 826 const cf = WebInspector.Color.Format; | 807 const cf = WebInspector.Color.Format; |
| 827 var format; | 808 var format; |
| 828 var formatSetting = WebInspector.moduleSetting("colorFormat").get(); | 809 var formatSetting = WebInspector.moduleSetting("colorFormat").get(); |
| 829 if (formatSetting === cf.Original) | 810 if (formatSetting === cf.Original) |
| 830 format = cf.Original; | 811 format = cf.Original; |
| 831 else if (formatSetting === cf.RGB) | 812 else if (formatSetting === cf.RGB) |
| 832 format = (color.hasAlpha() ? cf.RGBA : cf.RGB); | 813 format = (color.hasAlpha() ? cf.RGBA : cf.RGB); |
| 833 else if (formatSetting === cf.HSL) | 814 else if (formatSetting === cf.HSL) |
| 834 format = (color.hasAlpha() ? cf.HSLA : cf.HSL); | 815 format = (color.hasAlpha() ? cf.HSLA : cf.HSL); |
| 835 else if (formatSetting === cf.HEX) | 816 else if (!color.hasAlpha()) |
| 836 format = color.detectHEXFormat(); | 817 format = (color.canBeShortHex() ? cf.ShortHEX : cf.HEX); |
| 837 else | 818 else |
| 838 format = cf.RGBA; | 819 format = cf.RGBA; |
| 839 | 820 |
| 840 return format; | 821 return format; |
| 841 } | 822 } |
| OLD | NEW |