mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-22 07:19:57 -05:00
One more nameToRGBColor() simplification, by reversing return logic.
This commit is contained in:
parent
a842b5faa6
commit
1c8c64c473
1 changed files with 23 additions and 25 deletions
|
@ -21,32 +21,30 @@ var Color = this.Color = Base.extend(new function() {
|
|||
|
||||
function nameToRGBColor(name) {
|
||||
var color = colorCache[name];
|
||||
if (!color) {
|
||||
// Use a 1x1 Canvas to draw to with the given name and then
|
||||
// retrieve the rgb values from. Build a cache for all these
|
||||
// colors on a per use basis.
|
||||
if (!colorContext) {
|
||||
var canvas = CanvasProvider.getCanvas(Size.create(1, 1));
|
||||
colorContext = canvas.getContext('2d');
|
||||
colorContext.globalCompositeOperation = 'copy';
|
||||
}
|
||||
// Set the fillStyle of the context to the passed name
|
||||
// and fill the canvas with it. Then retrieve the first pixel:
|
||||
colorContext.fillStyle = name;
|
||||
colorContext.fillRect(0, 0, 1, 1);
|
||||
var data = colorContext.getImageData(0, 0, 1, 1).data,
|
||||
rgb = [];
|
||||
for (var i = 0; i < 3; i++)
|
||||
rgb[i] = data[i] / 255;
|
||||
// If the name wasn't found, rgb is [0, 0, 0]
|
||||
if (rgb.join('') == '000' && name != 'black') {
|
||||
throw new Error('The named color "' + name
|
||||
+ '" does not exist.');
|
||||
} else {
|
||||
color = colorCache[name] = RGBColor.read(rgb);
|
||||
}
|
||||
if (color)
|
||||
return color;
|
||||
// Use a 1x1 Canvas to draw to with the given name and then
|
||||
// retrieve the rgb values from. Build a cache for all these
|
||||
// colors on a per use basis.
|
||||
if (!colorContext) {
|
||||
var canvas = CanvasProvider.getCanvas(Size.create(1, 1));
|
||||
colorContext = canvas.getContext('2d');
|
||||
colorContext.globalCompositeOperation = 'copy';
|
||||
}
|
||||
return color;
|
||||
// Set the fillStyle of the context to the passed name
|
||||
// and fill the canvas with it. Then retrieve the first pixel:
|
||||
colorContext.fillStyle = name;
|
||||
colorContext.fillRect(0, 0, 1, 1);
|
||||
var data = colorContext.getImageData(0, 0, 1, 1).data,
|
||||
rgb = [];
|
||||
for (var i = 0; i < 3; i++)
|
||||
rgb[i] = data[i] / 255;
|
||||
// If the name wasn't found, rgb is [0, 0, 0]
|
||||
if (rgb.join('') == '000' && name != 'black') {
|
||||
throw new Error('The named color "' + name
|
||||
+ '" does not exist.');
|
||||
}
|
||||
return (colorCache[name] = RGBColor.read(rgb));
|
||||
}
|
||||
|
||||
function hexToRGBColor(string) {
|
||||
|
|
Loading…
Reference in a new issue