|
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 } |
|
|
|
|
|
|