Class: Color::FullPalette

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

Instance Attribute Summary

Attributes inherited from Palette

#background, #brightness_end, #brightness_start, #brightness_step, #saturation_end, #saturation_start, #saturation_step

Instance Method Summary collapse

Methods inherited from Palette

#colors_by_hue, #print_as_html, #print_as_text, #print_html_color, #print_html_hue_line

Constructor Details

#initialize(hues, args = Hash.new) ⇒ FullPalette

Returns a new instance of FullPalette.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/color/full_palette.rb', line 15

def initialize(hues, args = Hash.new)
  super(args)

  # hue => saturation => brightness => rgb
  @colors = Array.new
  
  hues.each do |hue|
    (saturation_start .. saturation_end).step(saturation_step) do |sat|
      (brightness_start .. brightness_end).step(brightness_step) do |brt|
        hsb = HSB.new hue, sat / 100.0, brt / 100.0
        @colors << hsb
      end
    end
  end
end

Instance Method Details

#colorsObject



31
32
33
# File 'lib/color/full_palette.rb', line 31

def colors
  @colors
end


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/color/full_palette.rb', line 39

def print_html_tables(io = $stdout)
  n_columns = 1 + (@brightness_end - @brightness_start) / @brightness_step

  # want the output to be:

  # +-------------------------------------------------------+
  # + hue[0]                                                +
  # + +-----------------------------------------------------+
  # + + sat[0]                                              +
  # + +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ 
  # + + r/b + + r/b + + r/b + + r/b + + r/b + + r/b + + r/b +
  # + +     + +     + +     + +     + +     + +     + +     +
  # + +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+
  # + + sat[1]                                              +
  # + +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ 
  # + + r/b + + r/b + + r/b + + r/b + + r/b + + r/b + + r/b +
  # + +     + +     + +     + +     + +     + +     + +     +
  # + +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+
  # +-------------------------------------------------------+
  # + hue[1]                                                +
  # + +-----------------------------------------------------+
  # + + sat[0]                                              +
  # + +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ 
  # + + r/b + + r/b + + r/b + + r/b + + r/b + + r/b + + r/b +
  # + +     + +     + +     + +     + +     + +     + +     +
  # + +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+
  # + + sat[1]                                              +
  # + +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ 
  # + + r/b + + r/b + + r/b + + r/b + + r/b + + r/b + + r/b +
  # + +     + +     + +     + +     + +     + +     + +     +
  # + +-----+ +-----+ +-----+ +-----+ +-----+ +-----+ +-----+
  # +-------------------------------------------------------+

  cellwidth = 100 / n_columns

  level = 1
  
  colors_by_hue.sort.each do |hue, by_saturation|
    puts_indented io, 1, '<table width="100%">'

    puts_indented io, 2, '<tr>'
    
    textcolor = @background.nil? || @background.index(%r{[A-Z]\w}) ? "000000" : "FFFFFF"
    puts_indented io, 3, "<td style=\"color: \##{textcolor};\">hue #{hue} &deg;</td>"
    
    puts_indented io, 2, '</tr>'

    by_saturation.sort.each do |sat, by_brightness|
      puts_indented io, 2, '<tr>'
      puts_indented io, 3, '<td>'

      puts_indented io, 4, '<table width="100%">'
      puts_indented io, 5, '<tr>'
      puts_indented io, 6, "<td colspan=#{n_columns}>"

      puts_indented io, 7, "saturation: #{sat}"

      puts_indented io, 6, '</td>'
      puts_indented io, 5, '</tr>'

      puts_indented io, 5, '<tr>'

      by_brightness.sort.reverse.each do |brt, hsb|
        rgbstr = sprintf("%06x", hsb.to_rgb)
        
        puts_indented io, 6, "<td style=\"width: #{cellwidth}%; vertical-align: bottom; border:2px outset; border-collapse:collapse;\">"
        
        puts_indented io, 7, "<table width=\"100%\" border=\"0\" border-width=\"4px\">"
        
        stylestr = @background ? " style=\"color: \##{rgbstr};\"" : ""
        
        text = sprintf "\#%s; b: %3.2f", rgbstr, brt
          
        puts_indented io, 8, '<tr><td' + stylestr + '>' + text + '</td></tr'

        puts_indented io, 8, "<tr><td style=\"background-color: \##{rgbstr}; vertical-align: bottom;\"><br>&nbsp;</td></tr>"

        puts_indented io, 7, "</table>"
        puts_indented io, 6, "</td>"
      end

      puts_indented io, 5, '</tr>'

      puts_indented io, 4, '</table>'

      puts_indented io, 3, '</td>'
      puts_indented io, 2, '</tr>'

    end

    puts_indented io, 1, '</table>'
  end
  
  return nil if true
    

  colors_by_hue.sort.each do |hue, by_saturation|
    print_html_hue_line(io, hue, n_columns)

    io.puts "<table width=\"100%\">"
    io.puts "    <tr>"
  
    by_saturation.sort.each do |sat, by_brightness|
      by_brightness.sort.reverse.each do |brt, hsb|
        io.puts "    <tr>"

        # this is one color per row, for now
        print_html_color(io, sat, brt, hsb.to_rgb, 100 / n_columns)

        io.puts "    </tr>"
      end
    end
    io.puts "</table>"
    io.puts "<br/>"
  end
end

#puts_indented(io, level, msg) ⇒ Object



35
36
37
# File 'lib/color/full_palette.rb', line 35

def puts_indented(io, level, msg)
  io.puts((' ' * (4 * level)) + msg)
end