Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
interactive color for checking ANSI colors
Shoes.app :title => 'Shoes Colors', :width => 600, :height => 600 do
colors = Shoes::COLORS.to_a.sort_by { |k, v| v.to_s }
colors.each do |name, color|
flow :width => 0.33 do
background color
para strong(name), "\n", color, :stroke => (color.dark? ? white : black),
:margin => 4, :align => 'center'
end
end
end
require "term/ansicolor"
require "readline"
String.send(:include, Term::ANSIColor)
str = 'Ruby is not Gem!'
c = %w(red blue yellow green)
puts %w(Hello, try interactive colors!).map { |w| w.send(c.shift) }.join(" ")
while line = Readline.readline("> ", true)
begin
case line.strip
when /^(q|quit|exit|bye)$/
puts "bye bye!".blink.cyan
exit
when /^\s*=\s*/
str = line.sub($&, '')
puts str
when /^(help|h|colors|attrs)$/
print "input one or more attributes from followings: ex. bold red on_green\n\n"
puts Term::ANSIColor.attributes.map { |attr| "#{attr}".send(attr) }.join(" ")
print "\nto set a string, input string with prepend '='. ex. = Color is fun!\n"
when /^rainbow$/
c = %w(red green yellow blue magenta cyan white)
s = rand(2) < 1 ? str.chars : str.split(/\b/)
puts s.inject("") { |mem, chr| mem << chr.send(c[rand(c.length)]) }
else
puts line.split(/[,\s]+/).inject(str) { |mem, color| mem.send color }
end
rescue
puts "is that color?"
end
end
require "termcolor"
str = 'Ruby is not Gem!'
c = %w(red blue yellow green)
print %w(Hello, try interactive colors!).inject("") { |mem, w| mem << "<#{t=c.shift}>#{w}</#{t}> " }.termcolor, "\n> "
while line = gets.chomp!
begin
case line
when /^(q|quit|exit|bye)$/
print "bye!\n"
exit
when /^\s*=\s*/
str = line.sub($&, '')
print str, "\n> "
when /^(help|colors|attrs)$/
colors = HighLine.constants[7..-3]
print colors.inject("") { |mem, color| mem << "<#{color}>#{color}</#{color}> " }.termcolor, "\n> "
else
print line.split(/[,\s]+/).inject(str) { |mem, color| "<#{color}>#{mem}</#{color}>" }.termcolor, "\n> "
end
rescue
print "is that color?\n> "
end
end
#!/usr/bin/env ruby
include Math
class Float
def to_degree
res = self * (180 / PI)
res < 0 ? res + 360 : res
end
def round_to(n)
(self * 10**n).round / 10.0**n
end
end
def rgb_to_hsb(*rgb)
rgb = rgb.map(&:to_f)
[hue(rgb), saturation(rgb), brightness(rgb)].map { |v| v.round_to(2) }
end
def hue(rgb)
r, g, b = rgb
atan2(sqrt(3) * (g - b), 2 * r - g - b).to_degree
end
def saturation(rgb)
max, min = rgb.max, rgb.min
if max.zero? && min.zero?
0.0
else
(max - min) / max * 100
end
end
def brightness(rgb)
rgb.max / 2.55
end
def hsb_to_rgb(*hsb)
hue, sat, bri = hsb
max = bri * 2.55
min = (1 - sat/100) * max
decimal = hue/60.0 - (hue/60.0).floor
dec = max * (1 - decimal * sat/100)
inc = max * (1 - (1 - decimal) * sat/100)
rgb =
case (hue/60).to_i
when 0 then [max, inc, min]
when 1 then [dec, max, min]
when 2 then [min, max, inc]
when 3 then [min, dec, max]
when 4 then [inc, min, max]
when 5 then [max, min, inc]
end
rgb.map { |v| v.round_to(2) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.