-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Color.scala
383 lines (329 loc) · 11.5 KB
/
Color.scala
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright 2015 Creative Scala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package doodle
package core
import doodle.syntax.angle._
import doodle.syntax.normalized._
import doodle.syntax.unsignedByte._
sealed abstract class Color extends Product with Serializable {
import Color._
// Accessors ----------------------------------------------
def red: UnsignedByte =
this.toRGBA.r
def green: UnsignedByte =
this.toRGBA.g
def blue: UnsignedByte =
this.toRGBA.b
def hue: Angle =
this.toHSLA.h
def saturation: Normalized =
this.toHSLA.s
def lightness: Normalized =
this.toHSLA.l
def alpha: Normalized =
this match {
case RGBA(_, _, _, a) => a
case HSLA(_, _, _, a) => a
}
// Color manipulation ------------------------------------
/** Copies this color, changing the hue to the given value */
def hue(angle: Angle): Color =
this.toHSLA.copy(h = angle)
/** Copies this color, changing the saturation to the given value */
def saturation(s: Normalized): Color =
this.toHSLA.copy(s = s)
/** Copies this color, changing the lightness to the given value */
def lightness(l: Normalized): Color =
this.toHSLA.copy(l = l)
/** Copies this color, changing the alpha to the given value */
def alpha(a: Normalized): Color =
this.toHSLA.copy(a = a)
/** Rotate hue by the given angle */
def spin(angle: Angle) = {
val original = this.toHSLA
original.copy(h = original.h + angle)
}
/** Lighten the color by the given amount. This is an absolute amount, not an
* amount relative to the Color's current lightness. Lightness is clipped at
* Normalized.MaxValue
*/
def lighten(lightness: Normalized) = {
val original = this.toHSLA
original.copy(l = Normalized.clip(original.l + lightness))
}
/** Darken the color by the given amount. This is an absolute amount, not an
* amount relative to the Color's current lightness. Lightness is clipped at
* Normalized.MaxValue
*/
def darken(darkness: Normalized) = {
val original = this.toHSLA
original.copy(l = Normalized.clip(original.l - darkness))
}
/** Saturate the color by the given amount. This is an absolute amount, not an
* amount relative to the Color's current saturation. Saturation is clipped
* at Normalized.MaxValue
*/
def saturate(saturation: Normalized) = {
val original = this.toHSLA
original.copy(s = Normalized.clip(original.s + saturation))
}
/** Desaturate the color by the given amount. This is an absolute amount, not
* an amount relative to the Color's current saturation. Saturation is
* clipped at Normalized.MaxValue
*/
def desaturate(desaturation: Normalized) = {
val original = this.toHSLA
original.copy(s = Normalized.clip(original.s - desaturation))
}
/** Increase the alpha channel by the given amount. */
def fadeIn(opacity: Normalized) = {
val original = this.toHSLA
original.copy(a = Normalized.clip(original.a + opacity))
}
/** Decrease the alpha channel by the given amount. */
def fadeOut(opacity: Normalized) = {
val original = this.toHSLA
original.copy(a = Normalized.clip(original.a - opacity))
}
/** Lighten the color by the given *relative* amount. For example, calling
* `aColor.lightenBy(0.1.normalized)` increases the lightness by 10% of the
* current lightness.
*/
def lightenBy(lightness: Normalized) = {
val original = this.toHSLA
original.copy(l = Normalized.clip(original.l.get * (1 + lightness.get)))
}
/** Darken the color by the given *relative* amount. For example, calling
* `aColor.darkenBy(0.1.normalized)` decreases the lightness by 10% of the
* current lightness.
*/
def darkenBy(darkness: Normalized) = {
val original = this.toHSLA
original.copy(l = Normalized.clip(original.l.get * (1 - darkness.get)))
}
/** Saturate the color by the given *relative* amount. For example, calling
* `aColor.saturateBy(0.1.normalized` increases the saturation by 10% of the
* current saturation.
*/
def saturateBy(saturation: Normalized) = {
val original = this.toHSLA
original.copy(s = Normalized.clip(original.s.get * (1 + saturation.get)))
}
/** Desaturate the color by the given *relative* amount. For example, calling
* `aColor.desaturateBy(0.1.normalized` decreases the saturation by 10% of
* the current saturation.
*/
def desaturateBy(desaturation: Normalized) = {
val original = this.toHSLA
original.copy(s = Normalized.clip(original.s.get * (1 - desaturation.get)))
}
/** Increase the alpha channel by the given relative amount. */
def fadeInBy(opacity: Normalized) = {
val original = this.toHSLA
original.copy(a = Normalized.clip(original.a.get * (1 + opacity.get)))
}
/** Decrease the alpha channel by the given relative amount. */
def fadeOutBy(opacity: Normalized) = {
val original = this.toHSLA
original.copy(a = Normalized.clip(original.a.get * (1 - opacity.get)))
}
// Other -------------------------------------------------
/** True if this is approximately equal to that */
def ~=(that: Color): Boolean =
(this.toRGBA, that.toRGBA) match {
case (RGBA(r1, g1, b1, a1), RGBA(r2, g2, b2, a2)) =>
Math.abs(r1 - r2) < 2 &&
Math.abs(g1 - g2) < 2 &&
Math.abs(b1 - b2) < 2 &&
Math.abs(a1 - a2) < 0.1
}
def toHSLA: HSLA =
this match {
case RGBA(r, g, b, a) =>
val rNormalized = r.toNormalized
val gNormalized = g.toNormalized
val bNormalized = b.toNormalized
val cMax = rNormalized max gNormalized max bNormalized
val cMin = rNormalized min gNormalized min bNormalized
val delta = cMax - cMin
val unnormalizedHue =
if (cMax == rNormalized)
60 * (((gNormalized - bNormalized) / delta))
else if (cMax == gNormalized)
60 * (((bNormalized - rNormalized) / delta) + 2)
else
60 * (((rNormalized - gNormalized) / delta) + 4)
val hue = unnormalizedHue.degrees
val lightness = Normalized.clip((cMax + cMin) / 2)
val saturation =
if (delta == 0.0)
Normalized.MinValue
else
Normalized.clip(delta / (1 - Math.abs(2 * lightness.get - 1)))
HSLA(hue, saturation, lightness, a)
case HSLA(h, s, l, a) => HSLA(h, s, l, a)
}
def toRGBA: RGBA =
this match {
case RGBA(r, g, b, a) => RGBA(r, g, b, a)
case HSLA(h, s, l, a) =>
s.get match {
case 0 =>
val lightness = l.toUnsignedByte
RGBA(lightness, lightness, lightness, a)
case s =>
def hueToRgb(p: Double, q: Double, t: Normalized): Normalized = {
Normalized.wrap(t.get match {
case t if t < 1.0 / 6.0 => p + (q - p) * 6 * t
case t if t < 0.5 => q
case t if t < 2.0 / 3.0 => p + (q - p) * (2.0 / 3.0 - t) * 6
case _ => p
})
}
val lightness = l.get
val q =
if (lightness < 0.5)
lightness * (1 + s)
else
lightness + s - (lightness * s)
val p = 2 * lightness - q
val r = hueToRgb(p, q, Normalized.wrap((h + 120.degrees).toTurns))
val g = hueToRgb(p, q, Normalized.wrap(h.toTurns))
val b = hueToRgb(p, q, Normalized.wrap((h - 120.degrees).toTurns))
RGBA(r.toUnsignedByte, g.toUnsignedByte, b.toUnsignedByte, a)
}
}
}
object Color extends CommonColors {
final case class RGBA(
r: UnsignedByte,
g: UnsignedByte,
b: UnsignedByte,
a: Normalized
) extends Color
final case class HSLA(h: Angle, s: Normalized, l: Normalized, a: Normalized)
extends Color
def rgba(
r: UnsignedByte,
g: UnsignedByte,
b: UnsignedByte,
a: Normalized
): Color =
RGBA(r, g, b, a)
def rgba(r: Int, g: Int, b: Int, a: Double): Color =
RGBA(r.uByte, g.uByte, b.uByte, a.normalized)
def hsla(h: Angle, s: Double, l: Double, a: Double): Color =
HSLA(h, s.normalized, l.normalized, a.normalized)
def rgb(r: UnsignedByte, g: UnsignedByte, b: UnsignedByte): Color =
rgba(r, g, b, 1.0.normalized)
/** Construct a [[Color]] in terms of red, green, and blue components. The
* alpha value defaults to 1.0 (fully opaque).
*/
def rgb(r: Int, g: Int, b: Int): Color =
rgba(r, g, b, 1.0)
/** Construct a [[Color]] in terms of hue, saturation, and lightness
* components. The alpha value defaults to 1.0 (fully opaque).
*/
def hsl(h: Angle, s: Double, l: Double): Color =
hsla(h, s, l, 1.0)
/** Parse a hexadecimal `String`, as commonly used to specify colors on the
* web, into a `Color`. The following formats are supported:
*
* - RGB
* - #RGB
* - RGBA
* - #RGBA
* - RRGGBB
* - #RRGGBB
* - RRGGBBAA
* - #RRGGBBAA
*
* Throws `java.lang.IllegalArgumentException` if the input is not a
* supported format.
*/
def fromHex(hex: String): Color = {
def error(): Nothing =
throw new IllegalArgumentException(
s"The string $hex doesn't represent a CSS hex-color"
)
def parseHex(offset: Int): Int =
hex(offset) match {
case '0' => 0
case '1' => 1
case '2' => 2
case '3' => 3
case '4' => 4
case '5' => 5
case '6' => 6
case '7' => 7
case '8' => 8
case '9' => 9
case 'a' => 10
case 'A' => 10
case 'b' => 11
case 'B' => 11
case 'c' => 12
case 'C' => 12
case 'd' => 13
case 'D' => 13
case 'e' => 14
case 'E' => 14
case 'f' => 15
case 'F' => 15
case other => error()
}
def parseSingleHex(offset: Int): Int = {
val h = parseHex(offset)
(h * 16) + h
}
def parseDoubleHex(offset: Int): Int =
(parseHex(offset) * 16) + parseHex(offset + 1)
def parseSingleAlpha(offset: Int): Double =
parseSingleHex(offset).toDouble / 255.0
def parseDoubleAlpha(offset: Int): Double =
parseDoubleHex(offset).toDouble / 255.0
val offset = if hex(0) == '#' then 1 else 0
(hex.size - offset) match {
case 3 =>
Color.rgb(
parseSingleHex(0 + offset),
parseSingleHex(1 + offset),
parseSingleHex(2 + offset)
)
case 4 =>
Color.rgba(
parseSingleHex(0 + offset),
parseSingleHex(1 + offset),
parseSingleHex(2 + offset),
parseSingleAlpha(3 + offset)
)
case 6 =>
Color.rgb(
parseDoubleHex(0 + offset),
parseDoubleHex(2 + offset),
parseDoubleHex(4 + offset)
)
case 8 =>
Color.rgba(
parseDoubleHex(0 + offset),
parseDoubleHex(2 + offset),
parseDoubleHex(4 + offset),
parseDoubleAlpha(6 + offset)
)
case other => error()
}
}
}