Class: Mooncats::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/mooncats/image.rb,
lib/mooncats/composite.rb

Defined Under Namespace

Classes: Composite

Constant Summary collapse

COLORS_GENESIS_WHITE =
['#555555', '#d3d3d3', '#ffffff', '#aaaaaa', '#ff9999']
COLORS_GENESIS_BLACK =
['#555555', '#222222', '#111111', '#bbbbbb', '#ff9999']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(design: 0, colors: COLORS_GENESIS_WHITE, zoom: 1) ⇒ Image

Returns a new instance of Image.



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
# File 'lib/mooncats/image.rb', line 41

def initialize( design: 0,
                colors: COLORS_GENESIS_WHITE,
                zoom: 1 )

    ## puts "==> [Mooncats] Image.new zoom: #{zoom}"

    design =  if design.is_a?( String )
                 Design.parse( design )
              elsif design.is_a?( Array )
                 Design.new( design )
              elsif design.is_a?( Design )
                 design  ## pass through as is 1:1
              else  ## assume integer nuber
                 design_num = design  ## note: for convenience "porcelain" param is named design (NOT design_num)
                 Design.find( design_num )
              end

    ## note: first color (index 0) is always nil (default/white or transparent)
    colors = [ nil ] + parse_colors( colors )

    ## puts " colors:"
    ## pp colors

  @cat = ChunkyPNG::Image.new( design.width*zoom,
                               design.height*zoom,
                               ChunkyPNG::Color::WHITE ) # why? why not?

    design.each_with_index do |row, x|
      row.each_with_index do |color, y|
        if color > 0
          pixel = colors[ color ]
          zoom.times do |n|
            zoom.times do |m|
              @cat[n+zoom*x,m+zoom*y] = pixel
            end
          end
        end # has color?
      end # each row
    end # each data
end

Class Method Details

.derive_palette(r, g, b, invert: false) ⇒ Object

(static) helpers



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
# File 'lib/mooncats/image.rb', line 98

def self.derive_palette( r, g, b, invert: false )
  ## note: Color.rgb returns an Integer (e.g. 34113279 - true color or just hex rgba or?)
  rgb = ChunkyPNG::Color.rgb( r, g, b )

   # to_hsl(color, include_alpha = false) ⇒ Array<Fixnum>[0], ...
   #   Returns an array with the separate HSL components of a color.
  hsl = ChunkyPNG::Color.to_hsl( rgb )
  #=> [237, 0.9705882352941178, 0.26666666666666666]

  h = hsl[0]
  s = hsl[1]
  l = hsl[2]

  hx = h % 360
  hy = (h + 320) % 360
  #=> e.g. hx: 237, hy: 197

  c1 = ChunkyPNG::Color.from_hsl( hx, 1, 0.1 )
  if invert
    c4 = ChunkyPNG::Color.from_hsl( hx, 1, 0.2 )
    c5 = ChunkyPNG::Color.from_hsl( hx, 1, 0.45 )
    c2 = ChunkyPNG::Color.from_hsl( hx, 1, 0.7 )
    c3 = ChunkyPNG::Color.from_hsl( hy, 1, 0.8 )
  else
    c2 = ChunkyPNG::Color.from_hsl( hx, 1, 0.2 )
    c3 = ChunkyPNG::Color.from_hsl( hx, 1, 0.45 )
    c4 = ChunkyPNG::Color.from_hsl( hx, 1, 0.7 )
    c5 = ChunkyPNG::Color.from_hsl( hy, 1, 0.8 )
  end

  ## note: returns an array of Integers!!
  ## e.g. [144127, 354047, 779775, 1701707775, 2581790719]
  [c1, c2, c3, c4, c5]
end

.generate(id, zoom: 1) ⇒ Object Also known as: mint



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mooncats/image.rb', line 10

def self.generate( id, zoom: 1 )
  meta = Metadata.new( id )

  design = meta.design.to_i   # note: meta.design is a struct/object - keep/use a local int !!!

  colors = if meta.genesis?
              if design % 2 == 0 && meta.invert? ||
                 design % 2 == 1 && !meta.invert?
                 COLORS_GENESIS_WHITE
              else
                 COLORS_GENESIS_BLACK
              end
           else
             derive_palette( meta.r,
                             meta.g,
                             meta.b, invert: meta.invert? )
           end

  new( design: design,
       colors: colors,
       zoom:   zoom )
end

Instance Method Details

#heightObject



90
# File 'lib/mooncats/image.rb', line 90

def height()       @cat.height; end

#imageObject

return image ref - use a different name - why? why not?



93
# File 'lib/mooncats/image.rb', line 93

def image()        @cat; end

#parse_color(color) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/mooncats/image.rb', line 141

def parse_color( color )
  if color.is_a?( Integer )  ## e.g. Assumess ChunkyPNG::Color.rgb() or such
    color ## pass through as is 1:1
  elsif color.is_a?(String)
    ## note: return an Integer !!! (not a Color class or such!!! )
    ChunkyPNG::Color.from_hex( color )
  else
    raise ArgumentError, "unknown color format; cannot parse - expected rgb hex string e.g. d3d3d3"
  end
end

#parse_colors(colors) ⇒ Object

helpers



136
137
138
139
# File 'lib/mooncats/image.rb', line 136

def parse_colors( colors )
  ## convert into ChunkyPNG::Color
  colors.map { |color| parse_color( color ) }
end

#save(path, constraints = {}) ⇒ Object

(image) delegates

todo/check: add some more??


85
86
87
# File 'lib/mooncats/image.rb', line 85

def save( path, constraints = {} )
  @cat.save( path, constraints )
end

#widthObject



89
# File 'lib/mooncats/image.rb', line 89

def width()        @cat.width; end