Total Complexity | 7 |
Complexity/F | 1.4 |
Lines of Code | 43 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | export default class TriColorBlend { |
||
2 | |||
3 | constructor(clr1 = '#00C800', clr2 = '#FFFF00', clr3 = '#C80000') |
||
4 | { |
||
5 | this.clr1 = this.HexToRGB(clr1); |
||
6 | this.clr2 = this.HexToRGB(clr2); |
||
7 | this.clr3 = this.HexToRGB(clr3); |
||
8 | } |
||
9 | |||
10 | RGBToHex(r, g, b) |
||
11 | { |
||
12 | let bin = r << 16 | g << 8 | b; |
||
13 | return (function (h) { |
||
14 | return new Array(7 - h.length).join("0") + h |
||
15 | })(bin.toString(16).toUpperCase()) |
||
16 | } |
||
17 | |||
18 | HexToRGB(hex) |
||
19 | { |
||
20 | let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); |
||
21 | return result ? { |
||
22 | r: parseInt(result[1], 16), |
||
23 | g: parseInt(result[2], 16), |
||
24 | b: parseInt(result[3], 16) |
||
25 | } : null; |
||
26 | } |
||
27 | |||
28 | colorFromPercentage(val) |
||
29 | { |
||
30 | let startColor = this.clr1; |
||
31 | let endColor = this.clr2; |
||
32 | if (val >= 50) { |
||
33 | startColor = this.clr2; |
||
34 | endColor = this.clr3; |
||
35 | val = val - 50; |
||
36 | } |
||
37 | const multiplier = (val / 50); |
||
38 | const r = Math.round(startColor.r + multiplier * (endColor.r - startColor.r)); |
||
39 | const g = Math.round(startColor.g + multiplier * (endColor.g - startColor.g)); |
||
40 | const b = Math.round(startColor.b + multiplier * (endColor.b - startColor.b)); |
||
41 | return '#' + this.RGBToHex(r,g,b); |
||
42 | } |
||
43 | } |
||
44 |