-
Notifications
You must be signed in to change notification settings - Fork 2
/
modifiers.js
132 lines (123 loc) · 4.21 KB
/
modifiers.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* ES-TinyColor : Modification Functions
* ────────────────────────────────────────────────────────────────────────────
* Thanks to less.js for some of the basics here
* <https://github.com/cloudhead/less.js/blob/master/lib/less/functions.js>
*/
import TinyColor from './classes/tinycolor.js'
import {mathRound, clamp01, mathMax, mathMin} from './utilities.js'
/**
* Apply a modification conditionally
* @param {Function} action The modification function to apply
* @param {arguments} args Arguments passed to specified function
* @return {TinyColor} The modified color
*/
export function modify(action, args) {
const actions = {invert, desaturate, saturate, greyscale, lighten, brighten, darken, spin}
const color = actions[action](...args)
const [source] = args
source._r = color._r
source._g = color._g
source._b = color._b
source.setAlpha(color._a)
return source
}
/**
* Invert Color
* @param {TinyColor} color The color to invert
* @return {TinyColor} The inverted color
*/
export function invert(color) {
const rgb = new TinyColor(color).toRgb()
rgb.r = mathMax(0, mathMin(255, 255 - rgb.r))
rgb.g = mathMax(0, mathMin(255, 255 - rgb.g))
rgb.b = mathMax(0, mathMin(255, 255 - rgb.b))
return new TinyColor(rgb)
}
/**
* Desaturate Color
* @param {TinyColor} color The color to modify
* @param {Number} amount The amount to desaturate <= 100
* @return {TinyColor} The modified color
*/
export function desaturate(color, amount) {
amount = (amount === 0) ? 0 : (amount || 10)
const hsl = new TinyColor(color).toHsl()
hsl.s -= amount / 100
hsl.s = clamp01(hsl.s)
return new TinyColor(hsl)
}
/**
* Saturate color
* @param {TinyColor} color The color to modify
* @param {Number} amount The amount to saturate <= 100
* @return {TinyColor} The modified color
*/
export function saturate(color, amount) {
amount = (amount === 0) ? 0 : (amount || 10)
const hsl = new TinyColor(color).toHsl()
hsl.s += amount / 100
hsl.s = clamp01(hsl.s)
return new TinyColor(hsl)
}
/**
* Remove all chroma, leaving luminence
* @param {TinyColor} color The color to modify
* @return {TinyColor} The modified color
*/
export function greyscale(color) {
return new TinyColor(color).desaturate(100)
}
/**
* Lighten a color
* @param {TinyColor} color The color to modify
* @param {Number} amount The amount to ligten by <= 100
* @return {TinyColor} The modified color
*/
export function lighten(color, amount) {
amount = (amount === 0) ? 0 : (amount || 10)
const hsl = new TinyColor(color).toHsl()
hsl.l += amount / 100
hsl.l = clamp01(hsl.l)
return new TinyColor(hsl)
}
/**
* Brighten a color
* @param {TinyColor} color The color to modify
* @param {Number} amount The amount to brighten by <= 100
* @return {TinyColor} The modified color
*/
export function brighten(color, amount) {
amount = (amount === 0) ? 0 : (amount || 10)
const rgb = new TinyColor(color).toRgb()
rgb.r = mathMax(0, mathMin(255, rgb.r - mathRound(255 * -(amount / 100))))
rgb.g = mathMax(0, mathMin(255, rgb.g - mathRound(255 * -(amount / 100))))
rgb.b = mathMax(0, mathMin(255, rgb.b - mathRound(255 * -(amount / 100))))
return new TinyColor(rgb)
}
/**
* Darken a color
* @param {TinyColor} color The color to modify
* @param {Number} amount The amount to brighten by <= 100
* @return {TinyColor} The modified color
*/
export function darken(color, amount) {
amount = (amount === 0) ? 0 : (amount || 10)
const hsl = new TinyColor(color).toHsl()
hsl.l -= amount / 100
hsl.l = clamp01(hsl.l)
return new TinyColor(hsl)
}
/**
* Spin takes a positive or negative amount within [-360, 360] indicating the
* change of hue. Values outside of this range will be wrapped into this range.
* @param {TinyColor} color The color to modify
* @param {Number} amount Degrees to rotate hue by
* @return {TinyColor} The modified color
*/
export function spin(color, amount) {
const hsl = new TinyColor(color).toHsl()
const hue = (hsl.h + amount) % 360
hsl.h = hue < 0 ? 360 + hue : hue
return new TinyColor(hsl)
}