/
color-image.js
84 lines (68 loc) · 1.45 KB
/
color-image.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Module dependencies.
*/
var stylus = require('stylus')
, Canvas = require('canvas')
, nodes = stylus.nodes
, utils = stylus.utils
/**
* Expose `ColorImage`.
*/
exports = module.exports = ColorImage;
/**
* Create a new `ColorImage` node with the given `color`.
*
* @param {Color} color node
* @return {ColorImage}
* @api public
*/
exports.create = function(color){
utils.assertColor(color);
return new ColorImage(color);
};
/**
* Return the data URI for `colorImage`.
*
* @param {ColorImage} colorImage
* @return {String}
* @api public
*/
exports.dataURL = function(colorImage){
utils.assertType(colorImage, 'colorimage');
return new nodes.String(colorImage.toDataURL());
};
/**
* Initialize a new `ColorImage` node with the given arguments.
*
* @param {Color} color node
* @api private
*/
function ColorImage(color) {
this.color = color;
this.canvas = new Canvas(1, 1);
this.ctx = this.canvas.getContext('2d');
this.ctx.fillStyle = color.toString();
this.ctx.fillRect(0, 0, 1, 1);
};
/**
* Inherit from `nodes.Node.prototype`.
*/
ColorImage.prototype.__proto__ = nodes.Node.prototype;
/**
* Inspect the color.
*
* @return {String}
* @api private
*/
ColorImage.prototype.toString = function(){
return 'ColorImage(' + this.color.toString() + ')';
};
/**
* Return data URI string.
*
* @return {String}
* @api private
*/
ColorImage.prototype.toDataURL = function(){
return this.canvas.toDataURL();
};