Last active
November 22, 2020 18:12
-
-
Save username0x0a/94bb2f9050236619a55cd021f8b67713 to your computer and use it in GitHub Desktop.
Asset Catalog HEX colours export
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
# Path: .../Colors.xcassets | |
# Usage: ruby exportColours.rb | |
require 'json' | |
def parseHex(inp) | |
return inp[2..-1] if inp.index('0x') == 0 | |
ret = ((inp.to_f) * 255).to_i.to_s(16) | |
ret = '0'+ ret if ret.length == 1 | |
return ret | |
end | |
colors = { } | |
fs = `ls | grep colorset`.split "\n" | |
fs.each{|f| | |
c = f[0..f.index('.')-1] | |
j = `cat #{f}/Contents.json` | |
j = JSON.parse j | |
j['colors'].each{|cd| | |
next if cd['appearances'] && cd['appearances'][0]['value'] == 'dark' | |
colors[c] = cd['color']['components'] | |
} | |
} | |
colors.each{|k,v| | |
a = v['alpha'].to_f | |
r = g = b = nil | |
if v['white'] then | |
r = g = b = parseHex v['white'] | |
elsif v['red'] then | |
r = parseHex v['red'] | |
g = parseHex v['green'] | |
b = parseHex v['blue'] | |
end | |
puts "@\"#{k}\": UIColorFromRGBA(0x#{r}#{g}#{b}, #{a})," | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment