Class: Color::Palette
- Inherits:
-
Object
- Object
- Color::Palette
- Defined in:
- lib/color/palette.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#background ⇒ Object
readonly
Returns the value of attribute background.
-
#brightness_end ⇒ Object
readonly
Returns the value of attribute brightness_end.
-
#brightness_start ⇒ Object
readonly
Returns the value of attribute brightness_start.
-
#brightness_step ⇒ Object
readonly
Returns the value of attribute brightness_step.
-
#saturation_end ⇒ Object
readonly
Returns the value of attribute saturation_end.
-
#saturation_start ⇒ Object
readonly
Returns the value of attribute saturation_start.
-
#saturation_step ⇒ Object
readonly
Returns the value of attribute saturation_step.
Instance Method Summary collapse
-
#colors ⇒ Object
$$$ todo: change to return hsbs.
-
#colors_by_hue ⇒ Object
Returns nested hashes: hue => saturation => brightness => rgb.
-
#initialize(args = Hash.new) ⇒ Palette
constructor
A new instance of Palette.
- #print_as_html(io = $stdout) ⇒ Object
- #print_as_text(io = $stdout) ⇒ Object
- #print_html_color(io, saturation, brightness, rgb, cellwidth) ⇒ Object
- #print_html_hue_line(io, hue, colspan) ⇒ Object
Constructor Details
#initialize(args = Hash.new) ⇒ Palette
Returns a new instance of Palette.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/color/palette.rb', line 22 def initialize(args = Hash.new) @background = args[:background] @saturation_start = args[:saturation_start] || 0 @saturation_end = args[:saturation_end] || 100 @saturation_step = args[:saturation_step] || 25 @brightness_start = args[:brightness_start] || 0 @brightness_end = args[:brightness_end] || 100 @brightness_step = args[:brightness_step] || 25 end |
Instance Attribute Details
#background ⇒ Object (readonly)
Returns the value of attribute background.
14 15 16 |
# File 'lib/color/palette.rb', line 14 def background @background end |
#brightness_end ⇒ Object (readonly)
Returns the value of attribute brightness_end.
19 20 21 |
# File 'lib/color/palette.rb', line 19 def brightness_end @brightness_end end |
#brightness_start ⇒ Object (readonly)
Returns the value of attribute brightness_start.
18 19 20 |
# File 'lib/color/palette.rb', line 18 def brightness_start @brightness_start end |
#brightness_step ⇒ Object (readonly)
Returns the value of attribute brightness_step.
20 21 22 |
# File 'lib/color/palette.rb', line 20 def brightness_step @brightness_step end |
#saturation_end ⇒ Object (readonly)
Returns the value of attribute saturation_end.
16 17 18 |
# File 'lib/color/palette.rb', line 16 def saturation_end @saturation_end end |
#saturation_start ⇒ Object (readonly)
Returns the value of attribute saturation_start.
15 16 17 |
# File 'lib/color/palette.rb', line 15 def saturation_start @saturation_start end |
#saturation_step ⇒ Object (readonly)
Returns the value of attribute saturation_step.
17 18 19 |
# File 'lib/color/palette.rb', line 17 def saturation_step @saturation_step end |
Instance Method Details
#colors ⇒ Object
$$$ todo: change to return hsbs
86 87 88 |
# File 'lib/color/palette.rb', line 86 def colors raise "abstract method: colors" end |
#colors_by_hue ⇒ Object
Returns nested hashes: hue => saturation => brightness => rgb
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/color/palette.rb', line 35 def colors_by_hue by_hue = Hash.new colors.each do |color| by_hue[color.hue] ||= Hash.new by_hue[color.hue][color.saturation] ||= Hash.new by_hue[color.hue][color.saturation][color.brightness] = color end by_hue end |
#print_as_html(io = $stdout) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/color/palette.rb', line 53 def print_as_html(io = $stdout) puts "@background: #{@background}" bg = @background || "FFFFFF" io.puts "<div style=\"background-color: " + bg + ";\">" print_html_tables(io) io.puts "</div>" end |
#print_as_text(io = $stdout) ⇒ Object
47 48 49 50 51 |
# File 'lib/color/palette.rb', line 47 def print_as_text(io = $stdout) colors.sort.each do |hsb| io.printf "%4.1f %3d %3d %06x\n", hsb.hue, hsb.saturation, hsb.brightness, hsb.to_rgb end end |
#print_html_color(io, saturation, brightness, rgb, cellwidth) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/color/palette.rb', line 70 def print_html_color(io, saturation, brightness, rgb, cellwidth) rgbstr = sprintf("%06x", rgb) io.puts " <td style=\"width: " + cellwidth.to_s + "%; vertical-align: bottom; border:2px outset; border-collapse:collapse;\">" io.puts " <table width=\"100%\" border=\"0\" border-width=\"4px\">" io.print " <tr><td" if @background print "style=\"color: " + rgbstr + ";\"" end io.printf ">#%s; s: %3.2f; b: %3.2f\n</td></tr>", rgbstr, saturation, brightness io.puts " <tr><td style=\"background-color: " + rgbstr + "; vertical-align: bottom;\"><br> </td></tr>" io.puts " </table>" io.puts " </td>" end |
#print_html_hue_line(io, hue, colspan) ⇒ Object
64 65 66 67 68 |
# File 'lib/color/palette.rb', line 64 def print_html_hue_line(io, hue, colspan) # if no background or is A+ (dark), then textcolor = @background.nil? || @background.index(%r{[A-Z]\w}) ? "000000" : "FFFFFF" io.puts " <td colspan=\"" + colspan.to_s + "\" style=\"color: #" + textcolor + ";\">hue " + hue.to_s + "°</td>" end |