aboutsummaryrefslogtreecommitdiff
path: root/node_modules/stylus/lib/functions/component.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/stylus/lib/functions/component.js')
-rw-r--r--node_modules/stylus/lib/functions/component.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/node_modules/stylus/lib/functions/component.js b/node_modules/stylus/lib/functions/component.js
new file mode 100644
index 00000000..37a9b237
--- /dev/null
+++ b/node_modules/stylus/lib/functions/component.js
@@ -0,0 +1,60 @@
+var utils = require('../utils')
+ , nodes = require('../nodes');
+
+/**
+ * Color component name map.
+ */
+
+var componentMap = {
+ red: 'r'
+ , green: 'g'
+ , blue: 'b'
+ , alpha: 'a'
+ , hue: 'h'
+ , saturation: 's'
+ , lightness: 'l'
+};
+
+/**
+ * Color component unit type map.
+ */
+
+var unitMap = {
+ hue: 'deg'
+ , saturation: '%'
+ , lightness: '%'
+};
+
+/**
+ * Color type map.
+ */
+
+var typeMap = {
+ red: 'rgba'
+ , blue: 'rgba'
+ , green: 'rgba'
+ , alpha: 'rgba'
+ , hue: 'hsla'
+ , saturation: 'hsla'
+ , lightness: 'hsla'
+};
+
+/**
+ * Return component `name` for the given `color`.
+ *
+ * @param {RGBA|HSLA} color
+ * @param {String} name
+ * @return {Unit}
+ * @api public
+ */
+
+module.exports = function component(color, name) {
+ utils.assertColor(color, 'color');
+ utils.assertString(name, 'name');
+ var name = name.string
+ , unit = unitMap[name]
+ , type = typeMap[name]
+ , name = componentMap[name];
+ if (!name) throw new Error('invalid color component "' + name + '"');
+ return new nodes.Unit(color[type][name], unit);
+};