colorist.tcl (4939B)
1 #!/usr/bin/env tclsh 2 3 proc array'reverse {oldName newName} { 4 upvar 1 $oldName old $newName new 5 foreach {key value} [array get old] {set new($value) $key} 6 } 7 8 # Xresources 9 array set from_xresources { 10 *.foreground: foreground 11 *.background: background 12 *.cursorColor: cursor 13 *.color0: black 14 *.color8: black_bright 15 *.color1: red 16 *.color9: red_bright 17 *.color2: green 18 *.color10: green_bright 19 *.color3: yellow 20 *.color11: yellow_bright 21 *.color4: blue 22 *.color12: blue_bright 23 *.color5: magenta 24 *.color13: magenta_bright 25 *.color6: cyan 26 *.color14: cyan_bright 27 *.color7: white 28 *.color15: white_bright 29 } 30 array'reverse from_xresources to_xresources 31 32 proc parse_xresources {filename} { 33 global from_xresources 34 set file [open $filename r] 35 set file_data [read $file] 36 close $file 37 set lines [split $file_data "\n"] 38 39 array set colors {} 40 foreach line $lines { 41 set line [string trim $line] 42 if { $line eq {} || [string first "!" $line] >= 0 } { 43 continue 44 } 45 set name [lindex $line 0] 46 set value [lindex $line 1] 47 if {[info exist from_xresources($name)]} { 48 set key $from_xresources($name) 49 set colors($key) $value 50 } 51 } 52 return [array get colors] 53 } 54 55 proc produce_xresources {_colors} { 56 global to_xresources 57 upvar $_colors colors 58 foreach color [array names colors] { 59 if {[info exist to_xresources($color)]} { 60 set key $to_xresources($color) 61 set value $colors($color) 62 puts "! $color" 63 puts "$key\t$value" 64 } 65 } 66 } 67 68 # Simple Terminal (suckless) 69 set st_colors [list white red green yellow blue magenta cyan black \ 70 white_bright red_bright green_bright yellow_bright blue_bright \ 71 magenta_bright cyan_bright black_bright] 72 set st_more_colors [list cursor background foreground background] 73 74 proc process_st_colors {keys _colors} { 75 upvar $_colors colors 76 foreach key $keys { 77 if {[info exist colors($key)]} { 78 set value $colors($key) 79 puts "\t\"$value\", // $key" 80 } else { 81 puts stderr "Missed color $key" 82 exit 1 83 } 84 } 85 } 86 87 proc produce_st {_colors} { 88 global st_colors st_more_colors 89 upvar $_colors colors 90 puts "static const char *colorname\[\] = \{" 91 process_st_colors $st_colors colors 92 puts "\t\[255\] = 0," 93 process_st_colors $st_more_colors colors 94 puts "\};" 95 } 96 97 # DWM 98 array set dwm_colors { 99 active_font background 100 inactive_font foreground \ 101 active_bg foreground 102 inactive_bg background 103 } 104 proc produce_dwm {_colors} { 105 global dwm_colors 106 upvar $_colors colors 107 foreach key [array names dwm_colors] { 108 set color $dwm_colors($key) 109 set value $colors($color) 110 puts "static const char $key\[\] = \"$value\";" 111 } 112 } 113 114 # dmenu 115 set dmenu_colors [list SchemeNorm background foreground \ 116 SchemeSel foreground background \ 117 SchemeOut red cyan] 118 proc produce_dmenu {_colors} { 119 global dmenu_colors 120 upvar $_colors colors 121 puts "static const char *colors\[SchemeLast\]\[2\] = \{" 122 foreach {name fg bg} $dmenu_colors { 123 set fg_color $colors($fg) 124 set bg_color $colors($bg) 125 puts "\t\[$name\] = \{ \"$fg_color\", \"$bg_color\" \}," 126 } 127 puts "\};" 128 } 129 130 # Xinit 131 proc produce_xinit {_colors} { 132 upvar $_colors colors 133 set color $colors(blue) 134 puts "xsetroot -solid \"$color\" &" 135 puts "exec dwm" 136 } 137 138 proc produce {file_format _colors} { 139 upvar $_colors colors 140 switch $file_format { 141 xres { 142 produce_xresources colors 143 } 144 st { 145 produce_st colors 146 } 147 dwm { 148 produce_dwm colors 149 } 150 dmenu { 151 produce_dmenu colors 152 } 153 xinit { 154 produce_xinit colors 155 } 156 default { 157 puts stderr "Unknown format." 158 exit 1 159 } 160 } 161 } 162 163 if {$argc < 3} { 164 puts "Usage:" 165 puts "\t$argv0 produce format color_scheme" 166 puts "\t$argv0 parse format file" 167 puts "\tformat: xres st dwm xinit" 168 exit 1 169 } 170 171 switch [lindex $argv 0] { 172 produce { 173 set file_format [lindex $argv 1] 174 set color_scheme [lindex $argv 2] 175 set file [open $color_scheme r] 176 set file_data [read $file] 177 close $file 178 array set colors [join $file_data] 179 produce $file_format colors 180 } 181 parse { 182 set file_format [lindex $argv 1] 183 set filename [lindex $argv 2] 184 if {$file_format ne "xres"} { 185 puts stderr "Unsuported format for parsing $file_format. Use xres." 186 exit 1 187 } 188 set scheme [parse_xresources $filename] 189 foreach {key value} $scheme { 190 puts "$key $value" 191 } 192 } 193 default { 194 puts stderr "unknown command" 195 exit 1 196 } 197 }