Class: Color::SamplePalette

Inherits:
Palette
  • Object
show all
Includes:
Loggable
Defined in:
lib/color/sample_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) ⇒ SamplePalette



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/color/sample_palette.rb', line 18

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

  n_samples        = args[:samples]
  n_total_samples  = args[:total_samples]
  n_per_hue        = if n_samples
                       hues.collect { n_samples }
                     else
                       Color::distribution(n_total_samples, hues.length)
                     end

  info "n_per_hue: #{n_per_hue}"
  
  @colors = Array.new

  hues.each_with_index do |hue, idx|
    n_per_hue[idx].times do
      # don't keep trying ...
      10.times do
        sat = Color::random_in_range(saturation_start, saturation_end, saturation_step)
        brt = Color::random_in_range(brightness_start, brightness_end, brightness_step)

        hsb = HSB.new hue, sat / 100.0, brt / 100.0

        if @colors.include?(hsb)
          # try, try again
        else
          @colors << hsb
          break
        end
      end
    end
  end
end

Instance Method Details

#colorsObject



53
54
55
# File 'lib/color/sample_palette.rb', line 53

def colors
  @colors
end


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/color/sample_palette.rb', line 57

def print_html_tables(io = $stdout)
  n_columns = 1 + (@brightness_end - @brightness_start) / @brightness_step
  
  colors_by_hue.sort.each do |hue, by_sat|
    io.puts "<table width=\"100%\">"
    io.puts "    <tr>"
    print_html_hue_line(io, hue, 1)
    io.puts "    </tr>"
    
    by_sat.sort.each do |sat, by_brt|
      by_brt.sort.reverse.each do |brt, hsb|
        io.puts "    <tr>"
        print_html_color(io, sat, brt, hsb.to_rgb, 100)
        io.puts "    </tr>"
      end
    end
    io.puts "</table>"
    io.puts "<br/>"
  end
end