-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.lisp
71 lines (65 loc) · 2.39 KB
/
common.lisp
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
(in-package #:cl-pkr)
(defun concat (&rest rest)
(apply #'concatenate 'string rest))
(defun pixel->color (pixel-list x y)
(if pixel-list
(funcall
#'(lambda (data) (mapcar
#'(lambda (i) (nth i (nth x (nth y data))))
'(0 1 2 3)))
pixel-list)
'(255 255 255 255)))
;; Modified from Emacs/color.el (2018 with GPL 3.0)
;; So, I have to make the whole project under GPL 3.0, I think
;; https://github.com/emacs-mirror/emacs/blob/master/lisp/color.el#L157
(defun color-rgb-to-hsl (red green blue)
"Convert RGB colors to their HSL representation.
Modified from Emacs/color.el with some ceiling and multiplication call,
it's a short function, so you can compare the source yourself."
(let* ((r red)
(g green)
(b blue)
(max (max r g b))
(min (min r g b))
(delta (- max min))
(l (/ (+ max min) 2.0)))
(if (= delta 0)
(list 0 0 (ceiling (* 100 l)))
(let* ((s (if (<= l 0.5) (/ delta (+ max min))
(/ delta (- 2.0 max min))))
(rc (/ (- max r) delta))
(gc (/ (- max g) delta))
(bc (/ (- max b) delta))
(h (mod
(/
(cond
((= r max) (- bc gc))
((= g max) (+ 2.0 rc (- bc)))
(t (+ 4.0 gc (- rc))))
6.0)
1.0)))
(list (ceiling (* h 360)) (ceiling (* 100 s)) (ceiling (* 100 l)))))))
(defun color->strs (color)
(setf color (subseq color 0 3))
(list
(apply #'format nil "#~2,'0X~2,'0X~2,'0X" color)
(apply #'format nil "~A, ~A, ~A" color)
(apply #'format nil "~A, ~A%, ~A%"
(apply #'color-rgb-to-hsl
(mapcar #'(lambda (c) (/ c 255)) color)))
(apply #'format nil "#~2,'0X~2,'0X~2,'0X"
(mapcar #'(lambda (c) (- 255 c)) color))))
(defun decimal->rgb (color)
(if (> color 0)
(let ((hex-str (format nil "~6,'0X" color)))
(loop for i from 0 to 5 by 2 collect
(parse-integer
(concatenate
'string
(string (char hex-str i))
(string (char hex-str (1+ i))))
:radix 16)))
'(0 0 0)))
(defun x-copy (text)
(with-atomic (format-wish "clipboard clear")
(format-wish "clipboard append \"~A\"" text)))