Passed
Push — develop ( 31af39...1ae34d )
by Andrew
26:09 queued 20:25
created

src/web/assets/src/js/tri-color-blend.js   A

Complexity

Total Complexity 7
Complexity/F 1.4

Size

Lines of Code 43
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 29
c 0
b 0
f 0
dl 0
loc 43
rs 10
mnd 2
bc 2
fnc 5
bpm 0.4
cpm 1.4
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A TriColorBlend.constructor 0 6 1
A TriColorBlend.colorFromPercentage 0 15 2
A TriColorBlend.HexToRGB 0 9 2
A TriColorBlend.RGBToHex 0 7 2
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