autocolors 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,159 @@
1
+ require 'matrix'
2
+
3
+ module Colors
4
+ LRGB2XYZ = Matrix[[0.4124,0.3576,0.1805],[0.2126,0.7152,0.0722],[0.0193,0.1192,0.9505]]
5
+ XYZ2LRGB = Matrix[[3.2406,-1.5372,-0.4986],[-0.9689,1.8758,0.0415],[0.0557,-0.2040,1.0570]]
6
+
7
+ # http://cs.haifa.ac.il/hagit/courses/ist/Lectures/Demos/ColorApplet2/t_convert.html
8
+ # http://en.wikipedia.org/wiki/Lab_color_space
9
+ class Color
10
+ attr_reader :rgb_approx
11
+
12
+ def initialize(lab,rgb=nil)
13
+ if rgb.nil?
14
+ @cl,@ca,@cb = lab.map{|v| v.to_f}
15
+ @lab_dirty = true
16
+ @rgb_dirty = false
17
+ else
18
+ @r,@g,@b = rgb.map{|v| v.to_f}
19
+ @lab_dirty = false
20
+ @rgb_dirty = true
21
+ end
22
+ end
23
+
24
+ # Intermediate spaces
25
+ def lr; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @lr end
26
+ def lg; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @lg end
27
+ def lb; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @lb end
28
+ def x; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @x end
29
+ def y; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @y end
30
+ def z; rgb_propagate if @rgb_dirty; lab_propagate if @lab_dirty; @z end
31
+
32
+ # CIELAB colorspace
33
+ def lab; rgb_propagate if @rgb_dirty; [@cl,@ca,@cb] end
34
+ def cl; rgb_propagate if @rgb_dirty; @cl end
35
+ def ca; rgb_propagate if @rgb_dirty; @ca end
36
+ def cb; rgb_propagate if @rgb_dirty; @cb end
37
+ def lab=(v) @lab_dirty=true; @cl,@ca,@cb=v.map{|a|a.to_f} end
38
+ def cl=(v) @lab_dirty=true; @cl=v.to_f end
39
+ def ca=(v) @lab_dirty=true; @ca=v.to_f end
40
+ def cb=(v) @lab_dirty=true; @cb=v.to_f end
41
+
42
+ # RGB colorspace
43
+ def rgb; lab_propagate if @lab_dirty; [@r,@g,@b] end
44
+ def r; lab_propagate if @lab_dirty; @r end
45
+ def g; lab_propagate if @lab_dirty; @g end
46
+ def b; lab_propagate if @lab_dirty; @b end
47
+ def rgb=(v) @rgb_dirty=true; @r,@g,@b=v.map{|a|a.to_f} end
48
+ def r=(v) @rgb_dirty=true; @r=v.to_f end
49
+ def g=(v) @rgb_dirty=true; @g=v.to_f end
50
+ def b=(v) @rgb_dirty=true; @b=v.to_f end
51
+
52
+ def to_s
53
+ '#' + rgb.map{|v|v.to_s(16).rjust(2,'0')}.join('')
54
+ end
55
+
56
+ def -(c)
57
+ if c.is_a?(Color)
58
+ # Euclidean distance in 3 dimensions, but emphasize brightness a bit
59
+ # more than the color (which is more how the human eye works)
60
+ ((cl*2.5 - c.cl*2.5)**2 + (ca - c.ca)**2 + (cb - c.cb)**2)**0.5
61
+ end
62
+ end
63
+
64
+ protected
65
+
66
+ def rgb_propagate
67
+ @lr,@lg,@lb = [@r,@g,@b].map{|c|rgbc_to_lrgbc(c)}
68
+ @x,@y,@z = (LRGB2XYZ*Matrix[[@lr],[@lg],[@lb]]).to_a.flatten
69
+ @cl = (@y > 0.008856) ? 116.0*(@y ** (1.0/3.0)) - 16.0 : 903.3 * @y
70
+ @ca = 500.0 * (labf(@x) - labf(@y))
71
+ @cb = 200.0 * (labf(@y) - labf(@z))
72
+ @rgb_dirty = false
73
+ end
74
+
75
+ def lab_propagate
76
+ p = (@cl + 16.0) / 116.0
77
+ @x = (p + @ca / 500.0) ** 3.0
78
+ @y = p ** 3.0
79
+ @z = (p - @cb / 200.0) ** 3.0
80
+ @lr,@lg,@lb = (XYZ2LRGB*Matrix[[@x],[@y],[@z]]).to_a.flatten
81
+ @rgb_approx = false
82
+ @r,@g,@b = [@lr,@lg,@lb].map{|c|lrgbc_to_rgbc(c)}
83
+ @lab_dirty = false
84
+ end
85
+
86
+ def labf(t)
87
+ t > 0.008856 ? t**(1.0/3.0) : 7.787 * t + (16.0/116.0)
88
+ end
89
+
90
+ # http://en.wikipedia.org/wiki/SRGB_color_space
91
+ def rgbc_to_lrgbc(c)
92
+ cf = c.to_f / 255.0
93
+ if cf <= 0.04045 then cf / 12.92
94
+ else ((cf + 0.055) / 1.055) ** 2.4 end
95
+ end
96
+
97
+ def lrgbc_to_rgbc(c)
98
+ if c <= 0.0031308 then v = (12.92 * c * 255.0).round
99
+ else v = ((1.055 * (c ** (1.0/2.4)) - 0.055) * 255.0).round end
100
+ if v < 0 then @rgb_approx = true; 0
101
+ elsif v > 255 then @rgb_approx = true; 255
102
+ else v end
103
+ end
104
+ end
105
+
106
+ # Primary 3-bit (8 colors). Unique representation!
107
+ CUBE256_8NORM = [[0x00,0x00,0x00],[0x80,0x00,0x00],[0x00,0x80,0x00],[0x80,0x80,0x00],
108
+ [0x00,0x00,0x80],[0x80,0x00,0x80],[0x00,0x80,0x80],[0xc0,0xc0,0xc0]]
109
+ # Equivalent "bright" versions of original 8 colors.
110
+ CUBE256_BRIGHT= [[0x80,0x80,0x80],[0xff,0x00,0x00],[0x00,0xff,0x00],[0xff,0xff,0x00],
111
+ [0x00,0x00,0xff],[0xff,0x00,0xff],[0x00,0xff,0xff],[0xff,0xff,0xff]]
112
+ # Strictly ascending.
113
+ CUBE256_STEPS = [0x00,0x5f,0x87,0xaf,0xd7,0xff]
114
+ # Gray-scale range.
115
+ CUBE256_GRAYS = [[0x08,0x08,0x08],[0x12,0x12,0x12],[0x1c,0x1c,0x1c],[0x26,0x26,0x26],
116
+ [0x30,0x30,0x30],[0x3a,0x3a,0x3a],[0x44,0x44,0x44],[0x4e,0x4e,0x4e],
117
+ [0x58,0x58,0x58],[0x62,0x62,0x62],[0x6c,0x6c,0x6c],[0x76,0x76,0x76],
118
+ [0x80,0x80,0x80],[0x8a,0x8a,0x8a],[0x94,0x94,0x94],[0x9e,0x9e,0x9e],
119
+ [0xa8,0xa8,0xa8],[0xb2,0xb2,0xb2],[0xbc,0xbc,0xbc],[0xc6,0xc6,0xc6],
120
+ [0xd0,0xd0,0xd0],[0xda,0xda,0xda],[0xe4,0xe4,0xe4],[0xee,0xee,0xee]]
121
+
122
+ CUBE256 = CUBE256_8NORM.map {|rgb| Colors::Color.new(rgb)} +
123
+ CUBE256_BRIGHT.map{|rgb| Colors::Color.new(rgb)} +
124
+ (0..(6*6*6 - 1)).map do |i|
125
+ b = i % 6; i = (i - b) / 6
126
+ g = i % 6; i = (i - g) / 6
127
+ Colors::Color.new([CUBE256_STEPS[i % 6],
128
+ CUBE256_STEPS[g],
129
+ CUBE256_STEPS[b]])
130
+ end +
131
+ CUBE256_GRAYS.map{|rgb| Colors::Color.new(rgb)}
132
+
133
+ class Color
134
+ def to_256
135
+ CUBE256.each_with_index.map{|c,i| [i,c-self]}.sort_by{|i,diff| diff}.first[0]
136
+ #CUBE256.sort_by{|c| c - self}.first
137
+ end
138
+
139
+ def to_16
140
+ CUBE256[0..15].each_with_index.map{|c,i| [i,c-self]}.sort_by{|i,diff| diff}.first[0]
141
+ end
142
+
143
+ def s_term
144
+ 'NONE'
145
+ end
146
+
147
+ def s_lcterm
148
+ 'NONE'
149
+ end
150
+
151
+ def s_cterm
152
+ 'NONE'
153
+ end
154
+
155
+ def s_high
156
+ 'NONE'
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,121 @@
1
+ require 'rubygems'
2
+ require 'webster'
3
+ require 'colors'
4
+
5
+ class DeepHash < Hash
6
+ def [](key)
7
+ self[key] = DeepHash.new unless has_key?(key)
8
+ super(key)
9
+ end
10
+
11
+ def nil?; return (super || keys.size == 0) end
12
+ end
13
+
14
+ class ColorScheme
15
+ attr_accessor :name, :contrast, :saturation
16
+
17
+ def initialize(name=nil)
18
+ @lights = DeepHash.new
19
+ @darks = DeepHash.new
20
+ srand rand(0xffffffff)
21
+ name ||= Webster.new.random_word
22
+ @name = name
23
+ @seed = @name.hash
24
+ srand @seed
25
+ generate
26
+ end
27
+
28
+ def [](key)
29
+ if key == :dark
30
+ @darks
31
+ elsif key == :light
32
+ @lights
33
+ else
34
+ raise ArgumentError, 'only :dark or :light as first key'
35
+ end
36
+ end
37
+
38
+ protected
39
+ LOW3 = 0; LOW2 = 1; LOW1 = 2; NORML = 3; NORMH = 4; HIGH1 = 5; HIGH2 = 6; HIGH3 = 7
40
+
41
+ AUX = [:string,:keyword,:preprocessor,:variable,:function,:constant,:type,:invalid]
42
+ AUX_CH = {
43
+ :comment => [],
44
+ :delimiter => [],
45
+ :string => [:pod_text,:heredoc,:heredoc_target,:single_quote,:double_quote,:backtick,:interpolated,:regex,:other,:character,:escape],
46
+
47
+
48
+ def generate
49
+ bc = nrand(-1,2,-4,5)
50
+ sat = rand * 2.0
51
+ @contrast = bc # Value between -4.0 and 5.0 used to contract/spread out intensity values
52
+ @saturation = sat # Value between 0.0 and 2.0 used to contract/intensify color values
53
+
54
+ @intensity = [00-(bc*3), 20-(bc*2), 45-bc, 50, 60, 65+bc, 90+(bc*2), 110+(bc*3)]
55
+ @fcolor = [0.0, 0.1*sat, 0.2*sat, 0.4*sat, 0.8*sat, 1.6*sat, 3.2*sat, 6.4*sat]
56
+
57
+ @base_a = nrand(0.0, 40.0, -120.0, 120.0)
58
+ @base_b = nrand(0.0, 40.0, -120.0, 120.0)
59
+
60
+ map_color [:normal, :bg], LOW3, LOW2
61
+ map_color [:comment, :bg], LOW3, LOW2
62
+
63
+ map_color [:normal, :fg], HIGH1, NORML
64
+ map_color [:comment, :fg], LOW2, LOW2
65
+
66
+ @aux = AUX.inject({}){|h,name| h[name] = [rand_color, rand_color]; h}
67
+ @aux.each do |name, colors|
68
+ a,b = colors
69
+ map_color [name, :bg], LOW3, LOW2, a, b
70
+ map_color [name, :fg], NORMH, NORMH, a, b
71
+ end
72
+
73
+ #colors[:normal][:bg] = lab(LOW3, LOW2 )
74
+ #colors[:normal][:fg] = lab(HIGH1,NORML)
75
+ end
76
+
77
+ def dark_i(idx) idx end
78
+ def light_i(idx) 7 - idx end
79
+
80
+ def map_color(mapping, intensity, saturation, a=@base_a, b=@base_b, styles=[])
81
+ loc = @lights
82
+ mapping[0..-2].each{|m| loc = loc[m]}
83
+ loc[mapping.last] = lab(light_i(intensity),saturation,a,b)
84
+ loc = @darks
85
+ mapping[0..-2].each{|m| loc = loc[m]}
86
+ loc[mapping.last] = lab(dark_i(intensity),saturation,a,b)
87
+ end
88
+
89
+ def lab(intensity, saturation, a, b)
90
+ Colors::Color.new([@intensity[intensity], a * @fcolor[saturation], b * @fcolor[saturation]])
91
+ end
92
+
93
+ def rand_color; nrand(0.0, 40.0, -120.0, 120.0) end
94
+
95
+ def nrand(mean=0, stddev=nil, floor=nil, ceil=nil)
96
+ theta = 2 * Math::PI * rand
97
+ rho = Math.sqrt(-2 * Math.log(1 - rand))
98
+ scale = stddev * rho
99
+ res = (rand >= 0.5) ? Math.cos(theta) : Math.sin(theta)
100
+ res = mean.to_f + scale * res
101
+ res = floor if (! floor.nil?) && (res < floor)
102
+ res = ceil if (! ceil.nil?) && (res > ceil)
103
+ return res
104
+ end
105
+
106
+ def curve(start,count,step=1.0)
107
+ gr = (1.0 + Math.sqrt(5))
108
+ res = [start]
109
+ (1..count-1).each do |i|
110
+ start *= (gr ** step)
111
+ res << start / 2.0
112
+ end
113
+ return res
114
+ end
115
+ end
116
+
117
+ #schemes = (0..100).map{ColorScheme.new}.sort_by{|c|c.contrast}
118
+ scheme = ColorScheme.new
119
+ require 'erb'
120
+ template = ERB.new(IO.read('templates/colorscheme.vim'),nil,'-')
121
+ puts template.result(binding)
@@ -0,0 +1,64 @@
1
+ # Autocolor | FC | FGI | FGS | BC | BGI | BGS | STYLES | vim | textmate / redcar | pygments | emacs
2
+
3
+ | nontext | 1 | ++ | ~ | 1 | --- | ~ | N | NonText | markup.other | gx * | ?
4
+ | ignore | 1 | --- | -- | 1 | --- | -- | V | Ignore | markup.ignore * | invisible * | ?
5
+
6
+
7
+ | text | 1 | ++ | ~ | 1 | --- | -- | N | Normal | markup | g * | font-face??
8
+ | text.whitespace | < | < | < | <' | <- | <- | N | Whitespace * | markup.whitespace * | w | ?
9
+ | text.deleted | <' | -- | <- | < | < | < | X | TextDeleted * | markup.deleted * | gd | ?
10
+ | text.inserted | <' | <+ | <+ | < | < | < | N | TextInserted * | markup.inserted * | gi | ?
11
+ | text.emphasis | < | < | < | < | < | < | I | TextEmphasis * | markup.italic | ge | ?
12
+ | text.strong | < | < | < | < | < | < | B | TextStrong * | markup.bold | gs | ?
13
+ | text.heading | <' | <+ | <+ | < | < | < | U'I'B' | TextHeading * | markup.heading | gh | ?
14
+ | text.subheading | <' | <+ | < | < | < | < | U'I'B' | TextSubheading * | markup.heading.2 | gu | ?
15
+ | text.output | <' | < | < | < | < | < | N | TextOutput * | markup.output * | go | ?
16
+
17
+
18
+ | comment | 1' | - | -- | 1 | --- | -- | I' | Comment | comment | c | comment-face
19
+ | comment.inline | <' | < | < | < | < | < | I' | CommentInline * | comment.line | c1 | ?
20
+ | comment.multiline | <' | < | < | < | < | < | I' | CommentBlock * | comment.block | cm | ?
21
+ | comment.special | <' | <+ | <+ | < | < | < | I'B' | SpecialComment | comment.block.documentation | cs | ?
22
+
23
+ | documentation | 1''| ++ | ~ | 1 | --- | -- | N | ? | comment.block.documentation | sd | doc-face + doc-string-face
24
+ | documentation.note | <' | < | < | < | < | < | N | ? | comment.block.documentation.note * | ? | ?
25
+ | documentation.todo | <' | < | < | < | < | < | N | Todo | comment.block.documentation.todo * | ? | ?
26
+ | documentation.version | <' | < | < | < | < | < | N | ? | comment.version * | v | ?
27
+
28
+
29
+ | literal | 2 | ++ | ~ | 1 | --- | -- | N | ? | ? | ? | ?
30
+
31
+ | lit.string | 2' | ++ | ~ | 1 | --- | -- | N | String | string | s | string-face
32
+ | lit.string.heredoc | <' | < | < | < | < | < | N | ? | string.unquoted | sh | ?
33
+ | lit.string.quoted | <' | < | < | < | < | < | N | ? | string.quoted | sx | ?
34
+ | lit.string.quoted.double | <' | < | < | < | < | < | N | ? | string.quoted.double | s2 | ?
35
+ | lit.string.quoted.single | <' | < | < | < | < | < | N | ? | string.quoted.single | s1 | ?
36
+ | lit.string.quoted.backtick | <' | < | < | < | < | < | N | ? | string.quoted.backtick * | sb | ?
37
+ | lit.string.interpolated | <' | < | < | < | <- | <+ | N | ? | string.interpolated | si | ?
38
+ | lit.string.regexp | <' | < | < | < | < | < | N | ? | string.regexp | sr | ?
39
+ | lit.string.char | <' | < | < | < | < | < | N | Character | constant.character | sc | ?
40
+ | lit.string.char.esc | 2' | <+ | < | < | < | < | U' | SpecialChar | constant.character.escape | se | ?
41
+ | lit.string.char.esc.unicode| <' | < | < | < | < | < | U' | ? | constant.character.escape.unicode *| ? | ?
42
+ | lit.string.char.esc.hex | <' | < | < | < | < | < | U' | ? | constant.character.escape.hex * | ? | ?
43
+ | lit.string.char.esc.oct | <' | < | < | < | < | < | U' | ? | constant.character.escape.oct * | ? | ?
44
+
45
+ | lit.number | 3 | ++ | ~ | 1 | --- | -- | N | Number | constant.numeric | m | ?
46
+ | lit.number.int | <' | < | < | < | < | < | N | ? | constant.numeric.integer * | mi | ?
47
+ | lit.number.int.hex | <' | < | < | < | < | < | N | ? | constant.numeric.integer
48
+ | lit.number.int.oct | <' | < | < | < | < | < | N | ? |
49
+ | lit.number.int.binary
50
+ | lit.number.int.long | <' | < | < | < | < | < | N | ? | constant.numeric.integer.long * | il | ?
51
+ | lit.number.float | <' | < | < | < | < | < | N | Float | constant.numeric.float * | mf | ?
52
+
53
+ | preproc.shebang |
54
+
55
+
56
+ | debug.errormsg | | | gr
57
+ | debug.traceback | | | gt
58
+
59
+
60
+ | delimiter |
61
+ | delimiter.comment | ? | ? | ? | comment-delimiter-face
62
+ | delimiter.heredoc
63
+ | delimiter.string
64
+ | delimiter.string.escape
@@ -0,0 +1,64 @@
1
+ <%
2
+ map = {
3
+ [:text] => 'Normal',
4
+ [:text, :misc] => 'NonText',
5
+ [:text, :whitespace] => 'Whitespace', # ***
6
+ [:text, :deleted] => 'TextDeleted',
7
+ [:text, :emph] => 'TextEmph',
8
+ [:text, :error] => 'TextError',
9
+ # ...
10
+ [:comment] => 'Comment',
11
+ }
12
+
13
+ map = {
14
+ 'Normal' => [:text, :normal],
15
+ 'Comment' => [:comment],
16
+ 'String' => [:literal,:string],
17
+ 'Statement' => [:keyword],
18
+ 'PreProc' => [:preprocessor],
19
+ 'Identifier' => [:variable],
20
+ 'Function' => [:function],
21
+ 'Constant' => [:constant],
22
+ 'Type' => [:type],
23
+ 'Error' => [:invalid],
24
+
25
+ 'Character' => [:literal,:string,:character],
26
+ 'SpecialChar' => [:literal,:string,:escape],
27
+ }
28
+ %>
29
+ if version > 580
30
+ hi clear
31
+ if exists("syntax_on")
32
+ syntax reset
33
+ endif
34
+ endif
35
+
36
+ let g:colors_name = expand('<sfile>:t:r')
37
+
38
+ if has("gui_running") || &t_Co > 16
39
+ if &background == "light"
40
+ <%- c = scheme[:light] -%>
41
+ <%- map.each do |vim_name, keys| cc = c; keys.each{|k| cc=cc[k]} -%>
42
+ hi <%= vim_name %> cterm=<%=cc[:fg].s_cterm %> gui=<%=cc[:fg].s_high %>
43
+ hi <%= vim_name %> ctermbg=<%=cc[:bg].to_256 %> ctermfg=<%= cc[:fg].to_256 %> guifg=<%=cc[:fg] %> guibg=<%=cc[:bg]%>
44
+ <%- end -%>
45
+ else
46
+ <%- c = scheme[:dark] -%>
47
+ <%- map.each do |vim_name, keys| cc = c; keys.each{|k| cc=cc[k]} -%>
48
+ hi <%= vim_name %> cterm=<%=cc[:fg].s_cterm %> gui=<%=cc[:fg].s_high %>
49
+ hi <%= vim_name %> ctermbg=<%=cc[:bg].to_256 %> ctermfg=<%= cc[:fg].to_256 %> guifg=<%=cc[:fg] %> guibg=<%=cc[:bg]%>
50
+ <%- end -%>
51
+ endif
52
+ else
53
+ if &background == "light"
54
+ <%- c = scheme[:light] -%>
55
+ <%- map.each do |vim_name, keys| cc = c; keys.each{|k| cc=cc[k]} -%>
56
+ hi <%=vim_name%> term=<%=cc[:fg].s_term%> cterm=<%=cc[:fg].s_lcterm%> ctermbg=<%=cc[:bg].to_16%> ctermfg=<%=cc[:fg].to_16%>
57
+ <%- end -%>
58
+ else
59
+ <%- c = scheme[:dark] -%>
60
+ <%- map.each do |vim_name, keys| cc = c; keys.each{|k| cc=cc[k]} -%>
61
+ hi <%=vim_name%> term=<%=cc[:fg].s_term%> cterm=<%=cc[:fg].s_lcterm%> ctermbg=<%=cc[:bg].to_16%> ctermfg=<%=cc[:fg].to_16%>
62
+ <%- end -%>
63
+ endif
64
+ endif
data/lib/autocolors.rb ADDED
@@ -0,0 +1,6 @@
1
+ module AutoColors
2
+ VERSION = File.exist?(File.join(File.dirname(__FILE__),'VERSION')) ? File.read(File.join(File.dirname(__FILE__),'VERSION')) : ""
3
+ class << self
4
+ def version() VERSION end
5
+ end
6
+ end