File size: 1,136 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

/* IMPORT */

import Color from '~/color';
import rgba from '~/methods/rgba';
import type {Channels} from '~/types';

/* MAIN */

//SOURCE: https://github.com/sass/dart-sass/blob/7457d2e9e7e623d9844ffd037a070cf32d39c348/lib/src/functions/color.dart#L718-L756

const mix = ( color1: string | Channels, color2: string | Channels, weight: number = 50 ): string => {

  const {r: r1, g: g1, b: b1, a: a1} = Color.parse ( color1 );
  const {r: r2, g: g2, b: b2, a: a2} = Color.parse ( color2 );
  const weightScale = weight / 100;
  const weightNormalized = ( weightScale * 2 ) - 1;
  const alphaDelta = a1 - a2;
  const weight1combined = ( ( weightNormalized * alphaDelta ) === -1 ) ? weightNormalized : ( weightNormalized + alphaDelta ) / ( 1 + weightNormalized * alphaDelta );
  const weight1 = ( weight1combined + 1 ) / 2;
  const weight2 = 1 - weight1;
  const r = ( r1 * weight1 ) + ( r2 * weight2 );
  const g = ( g1 * weight1 ) + ( g2 * weight2 );
  const b = ( b1 * weight1 ) + ( b2 * weight2 );
  const a = ( a1 * weightScale ) + ( a2 * ( 1 - weightScale ) );

  return rgba ( r, g, b, a );

};

/* EXPORT */

export default mix;