-
Notifications
You must be signed in to change notification settings - Fork 2
/
color.rkt
55 lines (47 loc) · 1.28 KB
/
color.rkt
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
#lang typed/racket
(provide (all-defined-out))
(require math/flonum)
(struct: byte-color
([red : Byte]
[green : Byte]
[blue : Byte])
#:transparent)
(struct: flcolor
([red : Flonum]
[green : Flonum]
[blue : Flonum]
[alpha : Flonum])
#:transparent)
(: flcolor->list (flcolor -> (Listof Flonum)))
(define (flcolor->list color)
(list (flcolor-red color)
(flcolor-green color)
(flcolor-blue color)
(flcolor-alpha color)))
(: flcolor->byte (Flonum -> Byte))
(define (flcolor->byte c)
(let ([b (max 0
(min 255
(inexact->exact
(round (fl* 255.0 c)))))])
(if (byte? b)
b
0)))
(: flcolor->byte-color (flcolor -> byte-color))
(define (flcolor->byte-color c)
(byte-color
(flcolor->byte (flcolor-red c))
(flcolor->byte (flcolor-green c))
(flcolor->byte (flcolor-blue c))))
(define flcolor-interpolate
(λ ([col-one : flcolor]
[col-two : flcolor]
[d : Flonum])
(let* ([1-d (fl- 1.0 d)]
[f (λ ([f : (flcolor -> Flonum)])
(fl+ (fl* 1-d (f col-one))
(fl* d (f col-two))))])
(flcolor (f flcolor-green)
(f flcolor-blue)
(f flcolor-red)
(f flcolor-alpha)))))