mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-08 05:42:07 -05:00
Optimize nameToRGBColor to also handle strings like rgb(0, 0, 0).
This commit is contained in:
parent
bfd43b27fa
commit
46ee4c517d
1 changed files with 6 additions and 8 deletions
|
@ -22,25 +22,23 @@ var Color = this.Color = Base.extend(new function() {
|
||||||
function nameToRGBColor(name) {
|
function nameToRGBColor(name) {
|
||||||
var color = colorCache[name];
|
var color = colorCache[name];
|
||||||
if (color)
|
if (color)
|
||||||
return color;
|
return color; // TODO: return a clone of the color
|
||||||
// Use a canvas to draw to with the given name and then retrieve the rgb
|
// Use a canvas to draw to with the given name and then retrieve rgb
|
||||||
// values from. Build a cache for all these colors on a per use basis.
|
// values from. Build a cache for all the used colors.
|
||||||
if (!colorContext) {
|
if (!colorContext) {
|
||||||
var canvas = CanvasProvider.getCanvas(Size.create(1, 1));
|
var canvas = CanvasProvider.getCanvas(Size.create(1, 1));
|
||||||
colorContext = canvas.getContext('2d');
|
colorContext = canvas.getContext('2d');
|
||||||
colorContext.globalCompositeOperation = 'copy';
|
colorContext.globalCompositeOperation = 'copy';
|
||||||
}
|
}
|
||||||
|
// Set the current fillStyle to transparent:
|
||||||
|
colorContext.fillStyle = 'rgba(0, 0, 0, 0)';
|
||||||
// Set the fillStyle of the context to the passed name and fill the
|
// Set the fillStyle of the context to the passed name and fill the
|
||||||
// canvas with it, then retrieve the data for the drawn pixel:
|
// canvas with it, then retrieve the data for the drawn pixel:
|
||||||
colorContext.fillStyle = name;
|
colorContext.fillStyle = name;
|
||||||
colorContext.fillRect(0, 0, 1, 1);
|
colorContext.fillRect(0, 0, 1, 1);
|
||||||
var data = colorContext.getImageData(0, 0, 1, 1).data,
|
var data = colorContext.getImageData(0, 0, 1, 1).data,
|
||||||
rgb = [data[0] / 255, data[1] / 255, data[2] / 255];
|
rgb = [data[0] / 255, data[1] / 255, data[2] / 255];
|
||||||
// If the name wasn't found, rgb is [0, 0, 0]
|
return colorCache[name] = RGBColor.read(rgb); // TODO: return a clone
|
||||||
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) {
|
function hexToRGBColor(string) {
|
||||||
|
|
Loading…
Reference in a new issue