-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcolor.rkt
49 lines (40 loc) · 1.47 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
#lang racket/base
(require rwind/base
rwind/doc-string
x11/x11
racket/dict
)
#|
- (in French) http://pficheux.free.fr/articles/lmf/xlib-images/
- List of existing colors:
$ tail /etc/X11/rgb.txt -n +2 | sed -e 's/^\s*//' -e 's/\s\s*/ /g'| cut -d' ' -f4,5
(colors are case insensitive)
- see also color-database<%> in Racket's help, but not all colors may be supported by default
|#
(define* black-pixel #f)
(define* white-pixel #f)
;; Cache the colors in a dictionary.
;; Maybe it would be a better idea to use Racket's own database...
;; (but not sure how to deal with that)
(define color-database (make-hash))
(define* (find-named-color color-str [default-color black-pixel])
(dict-ref! color-database color-str
(λ()
(define disp (current-display))
(define screen (DefaultScreen disp))
(AllocNamedColor disp screen color-str default-color))))
(define* (init-colors)
(define disp (current-display))
(define screen (DefaultScreen disp))
(set! black-pixel (BlackPixel disp screen))
(set! white-pixel (WhitePixel disp screen))
)
;; Not yet conform to XAllocColor...
#;(define* (random-dark-color)
(define disp (current-display))
(define screen (DefaultScreen disp))
(define cmap (DefaultColorMap disp screen))
(XAllocColor disp cmap
(shuffle (list (random 64)
(+ 64 (random 64))
(+ 64 (random 64))))))