Skip to content

Commit a295809

Browse files
Matt Wozniskivim-scripts
authored andcommitted
Version 4.00
Fix CSApprox to not fail in vim 7.3 if not +gui, now that vim behaves properly even without +gui Provide the :CSApprox command to re-run CSApprox's approximation algorithm even if the colorscheme hasn't changed - useful for when the user has tweaked some colors manually. Better handling for the inverse (aka reverse) attribute for terminals that actually support it - and add the g:CSApprox_fake_reverse config variable to allow switching back to the old behavior for terminals that don't support real reverse video. Fix an issue where CSApprox would unconditionally leave 'background' set to "light" - now it will leave 'background' unchanged when it runs. Change the handling for Konsole to use the xterm palette by for KDE versions >= 2.2.0 - Konsole itself was changed to drop its old, slightly incompatible palette in KDE 2.2.0 Fix a minor issue where running vim in recovery mode with -r would result in a complaint from CSApprox that the terminal didn't have enough colors even when it did. Fix an issue where, even if CSApprox had been disabled by setting g:CSApprox_loaded, a CSApprox error message could still be displayed.
1 parent ce2b1fd commit a295809

File tree

6 files changed

+435
-187
lines changed

6 files changed

+435
-187
lines changed

after/plugin/CSApprox.vim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
" Copyright (c) 2012, Matthew J. Wozniski
2+
" All rights reserved.
3+
"
4+
" Redistribution and use in source and binary forms, with or without
5+
" modification, are permitted provided that the following conditions are met:
6+
" * Redistributions of source code must retain the above copyright
7+
" notice, this list of conditions and the following disclaimer.
8+
" * Redistributions in binary form must reproduce the above copyright
9+
" notice, this list of conditions and the following disclaimer in the
10+
" documentation and/or other materials provided with the distribution.
11+
" * The names of the contributors may not be used to endorse or promote
12+
" products derived from this software without specific prior written
13+
" permission.
14+
"
15+
" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
16+
" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19+
" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
26+
" The last thing to do when sourced is to run and actually fix up the colors.
27+
if !has('gui_running') && exists(':CSApprox')
28+
CSApprox
29+
endif

autoload/csapprox.vim

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
" Copyright (c) 2012, Matthew J. Wozniski
2+
" All rights reserved.
3+
"
4+
" Redistribution and use in source and binary forms, with or without
5+
" modification, are permitted provided that the following conditions are met:
6+
" * Redistributions of source code must retain the above copyright
7+
" notice, this list of conditions and the following disclaimer.
8+
" * Redistributions in binary form must reproduce the above copyright
9+
" notice, this list of conditions and the following disclaimer in the
10+
" documentation and/or other materials provided with the distribution.
11+
" * The names of the contributors may not be used to endorse or promote
12+
" products derived from this software without specific prior written
13+
" permission.
14+
"
15+
" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
16+
" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19+
" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
126
let s:rgb = {}
227

328
let s:rgb["alice blue"] = "#f0f8ff"

autoload/csapprox/common.vim

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
" Copyright (c) 2012, Matthew J. Wozniski
2+
" All rights reserved.
3+
"
4+
" Redistribution and use in source and binary forms, with or without
5+
" modification, are permitted provided that the following conditions are met:
6+
" * Redistributions of source code must retain the above copyright
7+
" notice, this list of conditions and the following disclaimer.
8+
" * Redistributions in binary form must reproduce the above copyright
9+
" notice, this list of conditions and the following disclaimer in the
10+
" documentation and/or other materials provided with the distribution.
11+
" * The names of the contributors may not be used to endorse or promote
12+
" products derived from this software without specific prior written
13+
" permission.
14+
"
15+
" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
16+
" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19+
" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
26+
let s:xterm_colors = [ 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF ]
27+
let s:eterm_colors = [ 0x00, 0x2A, 0x55, 0x7F, 0xAA, 0xD4 ]
28+
let s:konsole_colors = [ 0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF ]
29+
let s:xterm_greys = [ 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A,
30+
\ 0x44, 0x4E, 0x58, 0x62, 0x6C, 0x76,
31+
\ 0x80, 0x8A, 0x94, 0x9E, 0xA8, 0xB2,
32+
\ 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE ]
33+
34+
let s:urxvt_colors = [ 0x00, 0x8B, 0xCD, 0xFF ]
35+
let s:urxvt_greys = [ 0x2E, 0x5C, 0x73, 0x8B,
36+
\ 0xA2, 0xB9, 0xD0, 0xE7 ]
37+
38+
" Uses &term to determine which cube should be use. If &term is set to
39+
" "xterm" or begins with "screen", the variables g:CSApprox_eterm and
40+
" g:CSApprox_konsole can be used to select a different palette.
41+
function! csapprox#common#PaletteType()
42+
if &t_Co == 88
43+
let type = 'urxvt'
44+
elseif &term ==# 'xterm' || &term =~# '^screen' || &term==# 'builtin_gui'
45+
if exists('g:CSApprox_konsole') && g:CSApprox_konsole
46+
let type = 'konsole'
47+
elseif exists('g:CSApprox_eterm') && g:CSApprox_eterm
48+
let type = 'eterm'
49+
else
50+
let type = 'xterm'
51+
endif
52+
elseif &term =~? '^konsole'
53+
" Konsole only used its own palette up til KDE 4.2.0
54+
if executable('kde4-config') && system('kde4-config --kde-version') =~ '^4\.[10]\.'
55+
let type = 'konsole'
56+
elseif executable('kde-config') && system('kde-config --version') =~# 'KDE: 3\.'
57+
let type = 'konsole'
58+
else
59+
let type = 'xterm'
60+
endif
61+
elseif &term =~? '^eterm'
62+
let type = 'eterm'
63+
else
64+
let type = 'xterm'
65+
endif
66+
67+
return type
68+
endfunction
69+
70+
" Retrieve the list of greyscale ramp colors for the current palette
71+
function! csapprox#common#Greys()
72+
return (&t_Co == 88 ? s:urxvt_greys : s:xterm_greys)
73+
endfunction
74+
75+
" Retrieve the list of non-greyscale ramp colors for the current palette
76+
function! csapprox#common#Colors()
77+
return s:{csapprox#common#PaletteType()}_colors
78+
endfunction
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
" Copyright (c) 2012, Matthew J. Wozniski
2+
" All rights reserved.
3+
"
4+
" Redistribution and use in source and binary forms, with or without
5+
" modification, are permitted provided that the following conditions are met:
6+
" * Redistributions of source code must retain the above copyright
7+
" notice, this list of conditions and the following disclaimer.
8+
" * Redistributions in binary form must reproduce the above copyright
9+
" notice, this list of conditions and the following disclaimer in the
10+
" documentation and/or other materials provided with the distribution.
11+
" * The names of the contributors may not be used to endorse or promote
12+
" products derived from this software without specific prior written
13+
" permission.
14+
"
15+
" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
16+
" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19+
" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
26+
" Integer comparator used to sort the complete list of possible colors
27+
function! s:IntCompare(i1, i2)
28+
return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1
29+
endfunc
30+
31+
" Color comparator to find the nearest element to a given one in a given list
32+
function! s:NearestElemInList(elem, list)
33+
let len = len(a:list)
34+
for i in range(len-1)
35+
if (a:elem <= (a:list[i] + a:list[i+1]) / 2)
36+
return a:list[i]
37+
endif
38+
endfor
39+
return a:list[len-1]
40+
endfunction
41+
42+
" Takes 3 decimal values for r, g, and b, and returns the closest cube number.
43+
"
44+
" This approximator considers closeness based upon the individiual components.
45+
" For each of r, g, and b, it finds the closest cube component available on
46+
" the cube. If the three closest matches can combine to form a valid color,
47+
" this color is used, otherwise we repeat the search with the greys removed,
48+
" meaning that the three new matches must make a valid color when combined.
49+
function! csapprox#per_component#Approximate(r,g,b)
50+
let hex = printf("%02x%02x%02x", a:r, a:g, a:b)
51+
52+
let colors = csapprox#common#Colors()
53+
let greys = csapprox#common#Greys()
54+
let type = csapprox#common#PaletteType()
55+
56+
if !exists('s:approximator_cache_'.type)
57+
let s:approximator_cache_{type} = {}
58+
endif
59+
60+
let rv = get(s:approximator_cache_{type}, hex, -1)
61+
if rv != -1
62+
return rv
63+
endif
64+
65+
" Only obtain sorted list once
66+
if !exists("s:".type."_greys_colors")
67+
let s:{type}_greys_colors = sort(greys + colors, "s:IntCompare")
68+
endif
69+
70+
let greys_colors = s:{type}_greys_colors
71+
72+
let r = s:NearestElemInList(a:r, greys_colors)
73+
let g = s:NearestElemInList(a:g, greys_colors)
74+
let b = s:NearestElemInList(a:b, greys_colors)
75+
76+
let len = len(colors)
77+
if (r == g && g == b && index(greys, r) != -1)
78+
let rv = 16 + len * len * len + index(greys, r)
79+
else
80+
let r = s:NearestElemInList(a:r, colors)
81+
let g = s:NearestElemInList(a:g, colors)
82+
let b = s:NearestElemInList(a:b, colors)
83+
let rv = index(colors, r) * len * len
84+
\ + index(colors, g) * len
85+
\ + index(colors, b)
86+
\ + 16
87+
endif
88+
89+
let s:approximator_cache_{type}[hex] = rv
90+
return rv
91+
endfunction

0 commit comments

Comments
 (0)