-
Notifications
You must be signed in to change notification settings - Fork 2
/
luminance.js
42 lines (31 loc) · 1.12 KB
/
luminance.js
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
// const color = [0, 0, 0]
const redLuminanceCoefficient = 0.2126
const greenLuminanceCoefficient = 0.7152
const blueLuminanceCoefficient = 0.0722
function getLuminance (color) {
return redLuminanceCoefficient * color[0] +
greenLuminanceCoefficient * color[1] +
blueLuminanceCoefficient * color[2]
}
function getColorWFixedLuminance (red, green, blue, targetLuminance) {
let luminanceCoefficient = 0
if (!(!!red || !!green || !!blue)) {
throw Error('One color must be omitted to calculate the new color')
}
if (!red) {
luminanceCoefficient = redLuminanceCoefficient
console.log('select red')
}
if (!green) {
luminanceCoefficient = greenLuminanceCoefficient
console.log('select green')
}
if (!blue) {
luminanceCoefficient = blueLuminanceCoefficient
console.log('select blue')
}
let tempLuminence = getLuminance([red || 0, green || 0, blue || 0])
if (tempLuminence > targetLuminance) throw Error('Target luminance is greater than provided color')
return (targetLuminance - tempLuminence) / luminanceCoefficient
}
module.exports = { getColorWFixedLuminance, getLuminance }