Skip to content

Instantly share code, notes, and snippets.

@NigelThorne
Last active October 20, 2015 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NigelThorne/6ab631abfea3264fae1e to your computer and use it in GitHub Desktop.
Save NigelThorne/6ab631abfea3264fae1e to your computer and use it in GitHub Desktop.
Invert brightness of #RRGGBB values references in text. Used to transform tmTheme files to with with NegativeScreen
require 'matrix'
@inversionmatrix = Matrix[
[ 0.3333333, -0.6666667, -0.6666667, 0.0000000, 0.0000000 ],
[ -0.6666667, 0.3333333, -0.6666667, 0.0000000, 0.0000000 ],
[ -0.6666667, -0.6666667, 0.3333333, 0.0000000, 0.0000000 ],
[ 0.0000000, 0.0000000, 0.0000000, 1.0000000, 0.0000000 ],
[ 1.0000000, 1.0000000, 1.0000000, 0.0000000, 1.0000000 ]]
def inv_m_str(str)
inv_m( str[0,2],str[2,2],str[4,2])
end
def inv_m(r,g,b,a = "ff")
to_hexstring(to_vector(r,g,b,a) * @inversionmatrix)
end
def to_hexstring(vector)
vector.to_a[0][0,3].map{|v| to_hex(v)}.join("")
end
def to_vector(r,g,b,a)
rd = to_dec(r)
gd = to_dec(g)
bd = to_dec(b)
ad = to_dec(a)
Matrix.row_vector([rd,gd,bd,ad,1.0])
end
def to_dec(hexstr)
hexstr.to_i(16).to_f / 0xff
end
def to_hex(decimal)
val = decimal * 0xff
return "00" if(decimal <=0 )
return "FF" if(decimal >= 255)
(val).to_i.to_s(16).upcase.rjust(2,'0')
end
puts ARGF.read.gsub(/(?<=#)[0-9a-fA-Z]{6}(?![0-9a-zA-Z])/){|match| inv_m_str match } #.gsub(/(?<=#)[0-9a-fA-Z]{3}(?![0-9a-zA-Z])/){|match| invertcharstring match }

Takes a file. Returns (stdout) a version with all references to colors in the format #rrggbb replaced with the inverse color (where inverse means the brightness is inverted)

I use this with NegativeScreen.exe and Chrome plugin "Deluminate" to make most apps have a black background.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment