mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-08 05:42:07 -05:00
Simplify nameToRGBColor() further: No need to create function in its own scope, simply use the inject scope to contain cache and context variables.
This commit is contained in:
parent
21aa12271e
commit
a842b5faa6
1 changed files with 30 additions and 30 deletions
|
@ -15,38 +15,38 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Color = this.Color = Base.extend(new function() {
|
var Color = this.Color = Base.extend(new function() {
|
||||||
var nameToRGBColor = new function() {
|
|
||||||
var cache = {},
|
var colorCache = {},
|
||||||
context;
|
colorContext;
|
||||||
return function(name) {
|
|
||||||
var color = cache[name];
|
function nameToRGBColor(name) {
|
||||||
if (!color) {
|
var color = colorCache[name];
|
||||||
// Use a 1x1 Canvas to draw to with the given name and then
|
if (!color) {
|
||||||
// retrieve the rgb values from. Build a cache for all these
|
// Use a 1x1 Canvas to draw to with the given name and then
|
||||||
// colors on a per use basis.
|
// retrieve the rgb values from. Build a cache for all these
|
||||||
if (!context) {
|
// colors on a per use basis.
|
||||||
var canvas = CanvasProvider.getCanvas(Size.create(1, 1));
|
if (!colorContext) {
|
||||||
context = canvas.getContext('2d');
|
var canvas = CanvasProvider.getCanvas(Size.create(1, 1));
|
||||||
context.globalCompositeOperation = 'copy';
|
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:
|
// Set the fillStyle of the context to the passed name
|
||||||
context.fillStyle = name;
|
// and fill the canvas with it. Then retrieve the first pixel:
|
||||||
context.fillRect(0, 0, 1, 1);
|
colorContext.fillStyle = name;
|
||||||
var data = context.getImageData(0, 0, 1, 1).data,
|
colorContext.fillRect(0, 0, 1, 1);
|
||||||
rgb = [];
|
var data = colorContext.getImageData(0, 0, 1, 1).data,
|
||||||
for (var i = 0; i < 3; i++)
|
rgb = [];
|
||||||
rgb[i] = data[i] / 255;
|
for (var i = 0; i < 3; i++)
|
||||||
// If the name wasn't found, rgb is [0, 0, 0]
|
rgb[i] = data[i] / 255;
|
||||||
if (rgb.join('') == '000' && name != 'black') {
|
// If the name wasn't found, rgb is [0, 0, 0]
|
||||||
throw new Error('The named color "' + name
|
if (rgb.join('') == '000' && name != 'black') {
|
||||||
+ '" does not exist.');
|
throw new Error('The named color "' + name
|
||||||
} else {
|
+ '" does not exist.');
|
||||||
color = cache[name] = RGBColor.read(rgb);
|
} else {
|
||||||
}
|
color = colorCache[name] = RGBColor.read(rgb);
|
||||||
}
|
}
|
||||||
return color;
|
|
||||||
}
|
}
|
||||||
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
function hexToRGBColor(string) {
|
function hexToRGBColor(string) {
|
||||||
|
|
Loading…
Reference in a new issue