class Colorin
Constants
- CUSTOM_COLORS
- DEFAULT_COLORS
- STYLES
- VERSION
Public Class Methods
color_palette()
click to toggle source
# File lib/colorin.rb, line 61 def self.color_palette DEFAULT_COLORS.keys.reject { |c| c.to_s.start_with? 'on_' }.map { |color| "#{Colorin.send "on_#{color}", ' ' } #{Colorin.send color, color.to_s}" } end
custom_color_palette()
click to toggle source
# File lib/colorin.rb, line 65 def self.custom_color_palette CUSTOM_COLORS.keys.map { |color| "#{Colorin.send "on_#{color}", ' ' } #{Colorin.send color, color.to_s}" } end
Public Instance Methods
hex(hex)
click to toggle source
# File lib/colorin.rb, line 53 def hex(hex) rgb *hex_to_rgb(hex) end
on_hex(hex)
click to toggle source
# File lib/colorin.rb, line 57 def on_hex(hex) on_rgb *hex_to_rgb(hex) end
on_rgb(r, g, b)
click to toggle source
# File lib/colorin.rb, line 49 def on_rgb(r, g, b) wrap "48;5;#{rgb_to_256(r, g, b)}" end
rgb(r, g, b)
click to toggle source
# File lib/colorin.rb, line 45 def rgb(r, g, b) wrap "38;5;#{rgb_to_256(r, g, b)}" end
Private Instance Methods
hex_to_rgb(hex)
click to toggle source
# File lib/colorin.rb, line 75 def hex_to_rgb(hex) raise ArgumentError, "Invalid hexadecimal: #{hex}" unless hex.match /[0-9abcdef]{6}/i [ hex[0,2].to_i(16), hex[2,2].to_i(16), hex[4,2].to_i(16), ] end
rgb_to_256(r, g, b)
click to toggle source
# File lib/colorin.rb, line 84 def rgb_to_256(r, g, b) raise ArgumentError, "Color out of range (0-255): r: #{r}, g: #{g}, b: #{b}" if [r,g,b].any? { |c| c < 0 || c > 255 } red, green, blue = [r, g, b].map { |c| (6 * (c / 256.0)).to_i } (red * 36 + green * 6 + blue + 16).abs end
wrap(code)
click to toggle source
# File lib/colorin.rb, line 71 def wrap(code) Colorin.new "\e[#{code}m#{self}\e[#{STYLES[:clear]}m" end