source: trip-planner-front/node_modules/stylus/lib/functions/transparentify.js@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.8 KB
Line 
1var utils = require('../utils')
2 , nodes = require('../nodes');
3
4/**
5 * Returns the transparent version of the given `top` color,
6 * as if it was blend over the given `bottom` color.
7 *
8 * Examples:
9 *
10 * transparentify(#808080)
11 * => rgba(0,0,0,0.5)
12 *
13 * transparentify(#414141, #000)
14 * => rgba(255,255,255,0.25)
15 *
16 * transparentify(#91974C, #F34949, 0.5)
17 * => rgba(47,229,79,0.5)
18 *
19 * @param {RGBA|HSLA} top
20 * @param {RGBA|HSLA} [bottom=#fff]
21 * @param {Unit} [alpha]
22 * @return {RGBA}
23 * @api public
24 */
25
26function transparentify(top, bottom, alpha){
27 utils.assertColor(top);
28 top = top.rgba;
29 // Handle default arguments
30 bottom = bottom || new nodes.RGBA(255, 255, 255, 1);
31 if (!alpha && bottom && !bottom.rgba) {
32 alpha = bottom;
33 bottom = new nodes.RGBA(255, 255, 255, 1);
34 }
35 utils.assertColor(bottom);
36 bottom = bottom.rgba;
37 var bestAlpha = ['r', 'g', 'b'].map(function(channel){
38 return (top[channel] - bottom[channel]) / ((0 < (top[channel] - bottom[channel]) ? 255 : 0) - bottom[channel]);
39 }).sort(function(a, b){return b - a;})[0];
40 if (alpha) {
41 utils.assertType(alpha, 'unit', 'alpha');
42 if ('%' == alpha.type) {
43 bestAlpha = alpha.val / 100;
44 } else if (!alpha.type) {
45 bestAlpha = alpha = alpha.val;
46 }
47 }
48 bestAlpha = Math.max(Math.min(bestAlpha, 1), 0);
49 // Calculate the resulting color
50 function processChannel(channel) {
51 if (0 == bestAlpha) {
52 return bottom[channel]
53 } else {
54 return bottom[channel] + (top[channel] - bottom[channel]) / bestAlpha
55 }
56 }
57 return new nodes.RGBA(
58 processChannel('r'),
59 processChannel('g'),
60 processChannel('b'),
61 Math.round(bestAlpha * 100) / 100
62 );
63}
64transparentify.params = ['top', 'bottom', 'alpha'];
65module.exports = transparentify;
Note: See TracBrowser for help on using the repository browser.