Class: Color::Palette

Inherits:
Object
  • Object
show all
Defined in:
lib/color/palette.rb

Direct Known Subclasses

FullPalette, SamplePalette

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#backgroundObject (readonly)

Returns the value of attribute background.



14
15
16
# File 'lib/color/palette.rb', line 14

def background
  @background
end

#brightness_endObject (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_startObject (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_stepObject (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_endObject (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_startObject (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_stepObject (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

#colorsObject

$$$ todo: change to return hsbs



86
87
88
# File 'lib/color/palette.rb', line 86

def colors
  raise "abstract method: colors"
end

#colors_by_hueObject

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


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


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


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>&nbsp;</td></tr>"
  io.puts "            </table>"
  io.puts  "        </td>"
end


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 + "&deg;</td>"
end