aboutsummaryrefslogtreecommitdiff
path: root/autoload/color.vim
blob: b5e0e8f686888a7de0eab7ba160117f20c19f8b9 (plain)
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
" File: color.vim
" Author: romgrk
" Date: 15 Mar 2016
" Description: vimscript RGB/HSL color parsing
" This is mostly a transcript from a javascript color parsing module.
" !::exe [so %]
" ================================================================================
" Definitions: {{{1
" In function names:
"  • “Hex” refers to hexadecimal color format e.g. #599eff
"  • “RGB” refers to an array of numbers [r, g, b]
"                                      where   r,g,b ∈ [0, 255]
"  • “HSL” refers to an array of floats  [h, s, l]
"                                      where   h,s,l ∈ [0, 1.0]
" }}}1
" ================================================================================
" Color-format patterns:
" {{{1

let s:patterns = {}

" 6 hex-numbers, optionnal #-prefix
let s:patterns['hex']      = '\v#?(\x{2})(\x{2})(\x{2})'

" short version is strict: starting # mandatory
let s:patterns['shortHex'] = '\v#(\x{1})(\x{1})(\x{1})'

" Disabled
"let s:patterns['rgb']  = '\vrgb\s*\((\d+)\s*,(\d+)\s*,(\d+)\s*)\s*'
"let s:patterns['rgba'] = '\vrgba\s*\((\d+)\s*,(\d+)\s*,(\d+)\s*,(\d+)\s*)\s*'

" }}}1
" ================================================================================
" Functions:
" {{{1

" @params  String       string   The string to test
" @returns Boolean     [0 or 1]  if string matches: rrggbb OR #rrggbb OR #rgb
fu! color#Test (string)
    for key in keys(s:patterns)
        if a:string =~# s:patterns[key]
            return 1
        end
    endfor
    return 0
endfu

" @params (r, g, b)
" @params ([r, g, b])
" @returns String           A RGB color
fu! color#RGBtoHex (...)
    let [r, g, b] = ( a:0==1 ? a:1 : a:000 )
    let num = printf('%02x', float2nr(r)) . ''
          \ . printf('%02x', float2nr(g)) . ''
          \ . printf('%02x', float2nr(b)) . ''
    return '#' . num
endfu

" @param {String|Number} color   The color to parse
fu! color#HexToRGB (color)
    if type(a:color) == 2
        let color = printf('%x', a:color)
    else
        let color = a:color | end

    let matches = matchlist(color, s:patterns['hex'])
    let factor  = 0x1

    if empty(matches)
        let matches = matchlist(color, s:patterns['shortHex'])
        let factor  = 0x10
    end

    if len(matches) < 4
        throw 'Couldnt parse ' . string(color) . ' ' .  string(matches)
    end

    let r = str2nr(matches[1], 16) * factor
    let g = str2nr(matches[2], 16) * factor
    let b = str2nr(matches[3], 16) * factor

    return [r, g, b]
endfu


" Converts an HSL color value to RGB. Conversion formula
" adapted from http://en.wikipedia.org/wiki/HSL_color_space.
" Assumes h, s, and l are contained in the set [0, 1] and
" returns r, g, and b in the set [0, 255].
" @param   Number  h     OR     @param  Array [h, s, l]
" @param   Number  s
" @param   Number  l
" @returns Array [r, g, b]     The RGB representation
fu! color#HSLtoRGB(...) " (h, s, l)
    let [h, s, l] = ( a:0==1 ? a:1 : a:000 )

    if (s == 0.0) " achromatic
        let r = l
        let g = l
        let b = l
    else
        let q = l < 0.5 ? l * (1 + s) : l + s - l * s
        let p = 2 * l - q
        let r = color#Hue2RGB(p, q, h + 0.33333)
        let g = color#Hue2RGB(p, q, h)
        let b = color#Hue2RGB(p, q, h - 0.33333)
    end

    return [r * 255.0, g * 255.0, b * 255.0]
endfu


" @param   Number  r     OR     @param  Array [r, g, b]
" @param   Number  g
" @param   Number  b
" @returns Array [h, s, l]     The HSL representation
fu! color#RGBtoHSL(...)
    let [r, g, b] = ( a:0==1 ? a:1 : a:000 )
    let max = max([r, g, b])
    let min = min([r, g, b])

    let r   = str2float(r)
    let g   = str2float(g)
    let b   = str2float(b)
    let max = str2float(max)
    let min = str2float(min)

    let max = max / 255
    let min = min / 255
    let r = r / 255
    let g = g / 255
    let b = b / 255
    let h = str2float(0)
    let s = str2float(0)
    let l = (max + min) / 2

    if (max == min)
        let h = 0   " achromatic
        let s = 0   " achromatic
    else
        let d = max - min
        let s = (l > 0.5 ? d / (2 - max - min)
                       \ : d / (max + min)     )
        if (max == r)
            let h = (g - b) / d + (g < b ? 6 : 0)
        end
        if (max == g)
            let h = (b - r) / d + 2
        end
        if (max == b)
            let h = (r - g) / d + 4
        end
        let h = h / 6
    end

    return [h, s, l]
endfu

fu! color#Hue2RGB(...) " (p, q, t)
    let [p, q, t] = ( a:0==1 ? a:1 : a:000 )

    if(t < 0) | let t += 1 | end
    if(t > 1) | let t -= 1 | end

    if(t < 1.0/6) | return (p + (q - p) * 6.0 * t)           | end
    if(t < 1.0/2) | return (q)                               | end
    if(t < 2.0/3) | return (p + (q - p) * (2.0/3 - t) * 6.0) | end

    return p
endfu

" }}}1
" ================================================================================
" Composed functions:
" {{{1

fu! color#HexToHSL (color)
    let [r, g, b] = color#HexToRGB(a:color)
    return color#RGBtoHSL(r, g, b)
endfu

fu! color#HSLtoHex (...)
    let [h, s, l] = ( a:0==1 ? a:1 : a:000 )
    let [r, g, b] = color#HSLtoRGB(h, s, l)
    return color#RGBtoHex(r, g, b)
endfu

" @params String                 color      The color
" @params {Number|String|Float} [amount=5]  The percentage of light
fu! color#Lighten(color, ...)
    let amount = a:0 ?
                \(type(a:1) < 2 ?
                    \str2float(a:1) : a:1 )
                \: 5.0

    if(amount < 1.0)
        let amount = 1.0 + amount
    else
        let amount = 1.0 + (amount / 100.0)
    end

    let rgb = color#HexToRGB(a:color)
    let rgb = map(rgb, 'v:val * amount')
    let rgb = map(rgb, 'v:val > 255.0 ? 255.0 : v:val')
    let rgb = map(rgb, 'float2nr(v:val)')
    let hex = color#RGBtoHex(rgb)
    return hex
endfu

" @params String                 color      The color
" @params {Number|String|Float} [amount=5]  The percentage of darkness
fu! color#Darken(color, ...)
    let amount = a:0 ?
                \(type(a:1) < 2 ?
                    \str2float(a:1) : a:1 )
                \: 5.0

    if(amount < 1.0)
        let amount = 1.0 - amount
    else
        let amount = 1.0 - (amount / 100.0)
    end

    if(amount < 0.0)
        let amount = 0.0 | end


    let rgb = color#HexToRGB(a:color)
    let rgb = map(rgb, 'v:val * amount')
    let rgb = map(rgb, 'v:val > 255.0 ? 255.0 : v:val')
    let rgb = map(rgb, 'float2nr(v:val)')
    let hex = color#RGBtoHex(rgb)
    return hex
endfu

function! color#Decrease (...)
    if &background == 'light'
        return call(function('color#Lighten'), a:000)
    end
    return call(function('color#Darken'), a:000)
endfunc

function! color#Increase (...)
    if &background == 'light'
        return call(function('color#Darken'), a:000)
    end
    return call(function('color#Lighten'), a:000)
endfunc

function! color#Mix (a, b, ...)
    let amount = a:0 ? a:1 : 0.5

    let ca = color#HexToRGB(a:a)
    let cb = color#HexToRGB(a:b)

    let r = s:interpolate(ca[0], cb[0], amount)
    let g = s:interpolate(ca[1], cb[1], amount)
    let b = s:interpolate(ca[2], cb[2], amount)

    return color#RGBtoHex([r, g, b])
endfunc

function! s:interpolate (start, end, amount)
    let diff = a:end - a:start
    return a:start + (diff * a:amount)
endfunc

" }}}1