Blame view

node_modules/stylus/lib/functions/transparentify.js 1.7 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
60
61
62
63
  var utils = require('../utils')
    , nodes = require('../nodes');
  
  /**
   * Returns the transparent version of the given `top` color,
   * as if it was blend over the given `bottom` color.
   *
   * Examples:
   *
   *     transparentify(#808080)
   *     => rgba(0,0,0,0.5)
   *
   *     transparentify(#414141, #000)
   *     => rgba(255,255,255,0.25)
   *
   *     transparentify(#91974C, #F34949, 0.5)
   *     => rgba(47,229,79,0.5)
   *
   * @param {RGBA|HSLA} top
   * @param {RGBA|HSLA} [bottom=#fff]
   * @param {Unit} [alpha]
   * @return {RGBA}
   * @api public
   */
  
  module.exports = function transparentify(top, bottom, alpha){
    utils.assertColor(top);
    top = top.rgba;
    // Handle default arguments
    bottom = bottom || new nodes.RGBA(255, 255, 255, 1);
    if (!alpha && bottom && !bottom.rgba) {
      alpha = bottom;
      bottom = new nodes.RGBA(255, 255, 255, 1);
    }
    utils.assertColor(bottom);
    bottom = bottom.rgba;
    var bestAlpha = ['r', 'g', 'b'].map(function(channel){
      return (top[channel] - bottom[channel]) / ((0 < (top[channel] - bottom[channel]) ? 255 : 0) - bottom[channel]);
    }).sort(function(a, b){return a < b;})[0];
    if (alpha) {
      utils.assertType(alpha, 'unit', 'alpha');
      if ('%' == alpha.type) {
        bestAlpha = alpha.val / 100;
      } else if (!alpha.type) {
        bestAlpha = alpha = alpha.val;
      }
    }
    bestAlpha = Math.max(Math.min(bestAlpha, 1), 0);
    // Calculate the resulting color
    function processChannel(channel) {
      if (0 == bestAlpha) {
        return bottom[channel]
      } else {
        return bottom[channel] + (top[channel] - bottom[channel]) / bestAlpha
      }
    }
    return new nodes.RGBA(
      processChannel('r'),
      processChannel('g'),
      processChannel('b'),
      Math.round(bestAlpha * 100) / 100
    );
  }