Created
November 6, 2010 13:07
-
-
Save melborne/665406 to your computer and use it in GitHub Desktop.
interactive color for checking ANSI colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "term/ansicolor" | |
require "readline" | |
String.send(:include, Term::ANSIColor) | |
str = 'Ruby is not Gem!' | |
ATTRS = Term::ANSIColor.attributes | |
C = (ATTRS*" ")[/red.*?white/].split(" ") | |
puts %w(Hello, try interactive colors!).map.with_index { |w, i| w.send(C[i]) }.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 <<-EOS | |
Input one or more attributes from followings: ex. bold red on_green | |
#{ATTRS.map { |attr| "#{attr}".send(attr) }.join(" ")} | |
To set a string, input string with prepend '='. ex. = Is Ruby a Jam? | |
Or try 'rainbow' to fun! | |
EOS | |
when /^rainbow$/ | |
s = [str.chars, str.split(/\b/), str.split(/\b/).reverse][rand(3)] | |
puts s.inject("") { |mem, chr| mem << chr.send(C[rand(C.length)]) } | |
else | |
inputs = line.split(/[,\s]+/) | |
raise unless (inputs.map(&:intern) - ATTRS).empty? | |
puts inputs.inject(str) { |mem, color| mem.send color } | |
end | |
rescue | |
puts "is that color?" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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