From 66f0e181f8962e6f05e2ca2ddecbaf8730a956b0 Mon Sep 17 00:00:00 2001 From: Agatha Rose Date: Tue, 8 Sep 2020 21:22:06 +0300 Subject: [PATCH] Replace default colors with an rgb gradient --- src/keyboard.cr | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/src/keyboard.cr b/src/keyboard.cr index f669230..805a382 100644 --- a/src/keyboard.cr +++ b/src/keyboard.cr @@ -10,33 +10,50 @@ class Keyboard @raw_layout = File.read(name) i = 0 c = @raw_layout.chars + # iterates over characters in layout while i < c.size # outputs a hashmap of characters inside [] in the layout and sets each to 0 uses if c[i] == '[' - until c[i+1] == ']' - # character escape - if c[i+1] == '\\' - i += 1 - end - @layout[c[i += 1]] = 0 + until c[i + 1] == ']' + # character escape + if c[i + 1] == '\\' + i += 1 end + @layout[c[i += 1]] = 0 + end end - i += 1 + i += 1 end end def to_s(io : IO) # remove character escapes formatted = @raw_layout.gsub(/\\(\S)/, &.[1]) - # defines heatmap colors - colors = %i(default light_yellow yellow light_red red) + + # an rgb gradient from purple to red + # defines the heatmap colors + colors = [ + {74, 16, 99}, + {113, 30, 114}, + {137, 40, 107}, + {160, 50, 100}, + {183, 60, 94}, + {207, 70, 87}, + {230, 80, 80}, + ] + # add a keypress heatmap - max = @layout.to_a.sort_by { |k, v| v }.reverse.to_h.values[0] - formatted = formatted.gsub(/\[(.{1,2})\]/) {|m| - # calculate proportion of key usage to colors - m.colorize.fore(:white).back(colors[((@layout[m.chars[1]] * (colors.size - 1)) / max).to_i]) + max = @layout.to_a.sort_by { |_, v| v }.reverse.to_h.values[0] + formatted = formatted.gsub(/\[(.{1,2})\]/) { |m| + # calculate proportion of key usage to colors + char_color = colors[((@layout[m.chars[1]] * (colors.size - 1)) / max).to_i] + + # set the key color + m.colorize.fore(:white).back(Colorize::ColorRGB.new(char_color[0].to_u8, char_color[1].to_u8, char_color[2].to_u8)) } + + # output the result io << formatted end end