Blame view

node_modules/stylus/lib/functions/rgba.js 1.44 KB
ce4c83ff   wxy   初始提交
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
  var utils = require('../utils')
    , nodes = require('../nodes');
  
  /**
   * Return a `RGBA` from the r,g,b,a channels.
   *
   * Examples:
   *
   *    rgba(255,0,0,0.5)
   *    // => rgba(255,0,0,0.5)
   *
   *    rgba(255,0,0,1)
   *    // => #ff0000
   *
   *    rgba(#ffcc00, 50%)
   *    // rgba(255,204,0,0.5)
   *
   * @param {Unit|RGBA|HSLA} red
   * @param {Unit} green
   * @param {Unit} blue
   * @param {Unit} alpha
   * @return {RGBA}
   * @api public
   */
  
  module.exports = function rgba(red, green, blue, alpha){
    switch (arguments.length) {
      case 1:
        utils.assertColor(red);
        return red.rgba;
      case 2:
        utils.assertColor(red);
        var color = red.rgba;
        utils.assertType(green, 'unit', 'alpha');
        alpha = green.clone();
        if ('%' == alpha.type) alpha.val /= 100;
        return new nodes.RGBA(
            color.r
          , color.g
          , color.b
          , alpha.val);
      default:
        utils.assertType(red, 'unit', 'red');
        utils.assertType(green, 'unit', 'green');
        utils.assertType(blue, 'unit', 'blue');
        utils.assertType(alpha, 'unit', 'alpha');
        var r = '%' == red.type ? Math.round(red.val * 2.55) : red.val
          , g = '%' == green.type ? Math.round(green.val * 2.55) : green.val
          , b = '%' == blue.type ? Math.round(blue.val * 2.55) : blue.val;
  
        alpha = alpha.clone();
        if (alpha && '%' == alpha.type) alpha.val /= 100;
        return new nodes.RGBA(
            r
          , g
          , b
          , alpha.val);
    }
  };