Created
September 12, 2023 19:29
-
-
Save J-Swift/1359584d1d5a214bae04b3dfe91d679a to your computer and use it in GitHub Desktop.
Convert iterm colors to hex
This file contains hidden or 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/env ruby | |
require 'json' | |
filename = 'iterm_colors.json' | |
mappings = [ | |
"black", | |
"red", | |
"green", | |
"yellow", | |
"blue", | |
"magenta", | |
"cyan", | |
"white", | |
] | |
json = JSON.parse(File.read(filename)) | |
def to_hex(val_double) | |
val_in_hex = (val_double * 255).round.to_s(16).rjust(2, '0').upcase | |
val_in_hex | |
end | |
def get_val(json_key) | |
json_key.match(/\d+/)[0].to_i rescue nil | |
end | |
sorted_keys = json.keys.sort_by { |it| get_val(it) || -1 } | |
sorted_keys.each do |key| | |
val = json[key] | |
r = to_hex(val["Red Component"]) | |
g = to_hex(val["Green Component"]) | |
b = to_hex(val["Blue Component"]) | |
rgb = "#{r}#{g}#{b}" | |
if ansi_val = get_val(key) | |
is_bright = ansi_val >= mappings.length | |
ansi_val = ansi_val % mappings.length | |
key = "#{is_bright ? 'Bright' : 'Regular'} #{mappings[ansi_val]}" | |
end | |
puts "[#{key}]: ##{rgb}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment