Created
October 26, 2012 22:45
-
-
Save Zetaeta/3962003 to your computer and use it in GitHub Desktop.
Converts XResources files containing terminal colours to Konsole colorscheme files. Does not support transparency.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
DEFAULT_BACKGROUND = 0x1C1C1C; | |
DEFAULT_FOREGROUND = 0xDCDCDC; | |
def main | |
colours = [] | |
hasBackground = false | |
hasForeground = false | |
if ARGV.length < 2 || ARGV.length > 3 | |
$stderr.puts "Usage: XResourcesToKonsole <XResources file> <Konsole colour scheme name> [Konsole colour scheme file]" | |
exit | |
end | |
inFile = File.new(ARGV[0], "r") | |
if ARGV.length == 3 | |
outFileName = ARGV[2] | |
else | |
outFileName = ENV["HOME"] + "/.kde4/share/apps/konsole/" + ARGV[1] + ".colorscheme" | |
end | |
outFile = File.new(outFileName, "w") | |
inFile.each do |line| | |
next if line.start_with?("!") | |
if line.start_with?("*color") | |
if line[7] == ":" | |
numString = line[6] | |
else | |
numString = line[6..7] | |
end | |
colourIndex = Integer(numString) | |
value = line.split(":")[1..-1].join.strip | |
colours[colourIndex] = toColour(value) | |
elsif line.start_with?("*background") | |
hasBackground = true | |
value = line.split(":")[1..-1].join.strip | |
background = toColour(value) | |
elsif line.start_with?("*foreground") | |
hasForeground = true | |
value = line.split(":")[1..-1].join.strip | |
foreground = toColour(value) | |
end | |
end | |
inFile.close | |
if !hasForeground | |
foreground = DEFAULT_FOREGROUND | |
end | |
if !hasBackground | |
background = DEFAULT_BACKGROUND | |
end | |
foregroundIntense = foreground | |
backgroundIntense = background | |
writeColour(outFile, "Background", background) | |
writeColour(outFile, "BackgroundIntense", background) | |
writeColour(outFile, "Foreground", foreground) | |
writeColour(outFile, "ForegroundIntense", foreground) | |
for i in 0..7 do | |
writeColour(outFile, "Color" + i.to_s, colours[i]) | |
writeColour(outFile, "Color" + i.to_s + "Intense", colours[i + 8]) | |
end | |
outFile.syswrite("[General]\nDescription=#{ARGV[1]}\n") | |
outFile.close | |
end | |
def writeColour(file, colourName, value) | |
file.syswrite("[" + colourName + "]\n" + toColourString(value) + "\n\n") | |
end | |
def toColourString(colour) | |
return "Color=" + ((colour >> 16) & 0xFF).to_s + "," + ((colour >> 8) & 0xFF).to_s + "," + (colour & 0xFF).to_s | |
end | |
def toColour(string) | |
if string.start_with?("#") | |
return Integer(string[1..-1], 16) | |
elsif string.start_with?("rgb") | |
colourPart = value.split(":")[1] | |
rgb = colourPart.split("/") | |
result = [] | |
i = 0 | |
rgb.each do |rgbPart| | |
result[i] = Integer(rgbPart, 16) % 256 | |
i += 1 | |
end | |
return (result[0] << 16) | (result[1] << 8) | result[2] | |
else | |
$stderr.puts "Unsupported colour format: " + string | |
return 0 | |
end | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment