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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 } | 54 } |
| 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", |
|
pfeldman
2016/05/19 07:14:06
Is there ShortHEXA?
samli
2016/05/20 00:22:18
Done.
| |
| 65 AlphaHEX: "alphahex", | |
|
dgozman
2016/05/19 07:10:59
I'd go for HEXA and ShortHEXA (which is missing he
pfeldman
2016/05/19 07:14:06
HEXA
samli
2016/05/20 00:22:18
Done.
| |
| 65 RGB: "rgb", | 66 RGB: "rgb", |
| 66 RGBA: "rgba", | 67 RGBA: "rgba", |
| 67 HSL: "hsl", | 68 HSL: "hsl", |
| 68 HSLA: "hsla" | 69 HSLA: "hsla" |
| 69 } | 70 } |
| 70 | 71 |
| 71 /** | 72 /** |
| 72 * @param {string} text | 73 * @param {string} text |
| 73 * @return {?WebInspector.Color} | 74 * @return {?WebInspector.Color} |
| 74 */ | 75 */ |
| 75 WebInspector.Color.parse = function(text) | 76 WebInspector.Color.parse = function(text) |
| 76 { | 77 { |
| 77 // Simple - #hex, rgb(), nickname, hsl() | 78 // Simple - #hex, rgb(), nickname, hsl() |
| 78 var value = text.toLowerCase().replace(/\s+/g, ""); | 79 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; | 80 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; |
|
pfeldman
2016/05/19 07:14:06
why different treatment of 3-4 ws 6-8?
samli
2016/05/20 00:22:18
"6,8" includes 7
"3,4" is just 3 & 4
| |
| 80 var match = value.match(simple); | 81 var match = value.match(simple); |
| 81 if (match) { | 82 if (match) { |
| 82 if (match[1]) { // hex | 83 if (match[1]) { // hex |
| 83 var hex = match[1].toLowerCase(); | 84 var hex = match[1].toLowerCase(); |
| 84 var format; | 85 var format; |
| 85 if (hex.length === 3) { | 86 if (hex.length === 3) { |
| 86 format = WebInspector.Color.Format.ShortHEX; | 87 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); | 88 hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt (1) + hex.charAt(2) + hex.charAt(2); |
| 88 } else | 89 } else if (hex.length === 4) { |
| 90 format = WebInspector.Color.Format.AlphaHEX; | |
| 91 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); | |
| 92 } else if (hex.length === 6) { | |
| 89 format = WebInspector.Color.Format.HEX; | 93 format = WebInspector.Color.Format.HEX; |
| 94 } else { | |
| 95 format = WebInspector.Color.Format.AlphaHEX; | |
| 96 } | |
| 90 var r = parseInt(hex.substring(0,2), 16); | 97 var r = parseInt(hex.substring(0,2), 16); |
| 91 var g = parseInt(hex.substring(2,4), 16); | 98 var g = parseInt(hex.substring(2,4), 16); |
| 92 var b = parseInt(hex.substring(4,6), 16); | 99 var b = parseInt(hex.substring(4,6), 16); |
| 93 return new WebInspector.Color([r / 255, g / 255, b / 255, 1], format , text); | 100 var a = format === WebInspector.Color.Format.AlphaHEX ? parseInt(hex .substring(6,8), 16) / 255 : 1; |
|
dgozman
2016/05/19 07:10:59
style: missing space after comma
samli
2016/05/20 00:22:18
Done.
| |
| 101 return new WebInspector.Color([r / 255, g / 255, b / 255, a], format , text); | |
| 94 } | 102 } |
| 95 | 103 |
| 96 if (match[2]) { // rgb | 104 if (match[2]) { // rgb |
| 97 var rgbString = match[2].split(/\s*,\s*/); | 105 var rgbString = match[2].split(/\s*,\s*/); |
| 98 var rgba = [ WebInspector.Color._parseRgbNumeric(rgbString[0]), | 106 var rgba = [ WebInspector.Color._parseRgbNumeric(rgbString[0]), |
| 99 WebInspector.Color._parseRgbNumeric(rgbString[1]), | 107 WebInspector.Color._parseRgbNumeric(rgbString[1]), |
| 100 WebInspector.Color._parseRgbNumeric(rgbString[2]), 1 ]; | 108 WebInspector.Color._parseRgbNumeric(rgbString[2]), 1 ]; |
| 101 return new WebInspector.Color(rgba, WebInspector.Color.Format.RGB, t ext); | 109 return new WebInspector.Color(rgba, WebInspector.Color.Format.RGB, t ext); |
| 102 } | 110 } |
| 103 | 111 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 249 * @return {boolean} | 257 * @return {boolean} |
| 250 */ | 258 */ |
| 251 hasAlpha: function() | 259 hasAlpha: function() |
| 252 { | 260 { |
| 253 return this._rgba[3] !== 1; | 261 return this._rgba[3] !== 1; |
| 254 }, | 262 }, |
| 255 | 263 |
| 256 /** | 264 /** |
| 257 * @return {boolean} | 265 * @return {boolean} |
| 258 */ | 266 */ |
| 259 canBeShortHex: function() | 267 canBeShortHex: function() |
|
dgozman
2016/05/19 07:10:59
I think this function is wrong now. We can replace
samli
2016/05/20 00:22:18
Done. Not all |hasAlpha| changed since asString()
| |
| 260 { | 268 { |
| 261 if (this.hasAlpha()) | 269 if (this.hasAlpha()) |
| 262 return false; | 270 return false; |
| 263 for (var i = 0; i < 3; ++i) { | 271 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 return false; |
| 267 } | 275 } |
| 268 return true; | 276 return true; |
| 269 }, | 277 }, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 case WebInspector.Color.Format.RGBA: | 325 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]); | 326 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: | 327 case WebInspector.Color.Format.HSL: |
| 320 if (this.hasAlpha()) | 328 if (this.hasAlpha()) |
| 321 return null; | 329 return null; |
| 322 var hsl = this.hsla(); | 330 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)); | 331 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: | 332 case WebInspector.Color.Format.HSLA: |
| 325 var hsla = this.hsla(); | 333 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]); | 334 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]); |
| 335 case WebInspector.Color.Format.AlphaHEX: | |
| 336 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();; | |
|
dgozman
2016/05/19 07:10:59
style: double semicolon
samli
2016/05/20 00:22:18
Done.
| |
| 327 case WebInspector.Color.Format.HEX: | 337 case WebInspector.Color.Format.HEX: |
| 328 if (this.hasAlpha()) | 338 if (this.hasAlpha()) |
| 329 return null; | 339 return null; |
| 330 return String.sprintf("#%s%s%s", toHexValue(this._rgba[0]), toHexVal ue(this._rgba[1]), toHexValue(this._rgba[2])).toLowerCase();; | 340 return String.sprintf("#%s%s%s", toHexValue(this._rgba[0]), toHexVal ue(this._rgba[1]), toHexValue(this._rgba[2])).toLowerCase();; |
| 331 case WebInspector.Color.Format.ShortHEX: | 341 case WebInspector.Color.Format.ShortHEX: |
| 332 if (!this.canBeShortHex()) | 342 if (!this.canBeShortHex()) |
| 333 return null; | 343 return null; |
| 334 return String.sprintf("#%s%s%s", toShortHexValue(this._rgba[0]), toS hortHexValue(this._rgba[1]), toShortHexValue(this._rgba[2])).toLowerCase();; | 344 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: | 345 case WebInspector.Color.Format.Nickname: |
| 336 return this.nickname(); | 346 return this.nickname(); |
| (...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 813 format = (color.hasAlpha() ? cf.RGBA : cf.RGB); | 823 format = (color.hasAlpha() ? cf.RGBA : cf.RGB); |
| 814 else if (formatSetting === cf.HSL) | 824 else if (formatSetting === cf.HSL) |
| 815 format = (color.hasAlpha() ? cf.HSLA : cf.HSL); | 825 format = (color.hasAlpha() ? cf.HSLA : cf.HSL); |
| 816 else if (!color.hasAlpha()) | 826 else if (!color.hasAlpha()) |
| 817 format = (color.canBeShortHex() ? cf.ShortHEX : cf.HEX); | 827 format = (color.canBeShortHex() ? cf.ShortHEX : cf.HEX); |
| 818 else | 828 else |
| 819 format = cf.RGBA; | 829 format = cf.RGBA; |
| 820 | 830 |
| 821 return format; | 831 return format; |
| 822 } | 832 } |
| OLD | NEW |