Class: Mooncats::Image
- Inherits:
-
Pixelart::Image
- Object
- Pixelart::Image
- Mooncats::Image
- 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
-
.derive_palette(r: nil, g: nil, b: nil, hue: nil, invert: false) ⇒ Object
(static) helpers.
- .generate(id) ⇒ Object (also: mint)
-
.read(path) ⇒ Object
convenience helper.
Instance Method Summary collapse
-
#initialize(initial = nil, design: nil, colors: nil) ⇒ Image
constructor
A new instance of Image.
Constructor Details
#initialize(initial = nil, design: nil, colors: nil) ⇒ Image
Returns a new instance of Image.
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 |
# File 'lib/mooncats/image.rb', line 48 def initialize( initial=nil, design: nil, colors: nil ) if initial ## pass image through as-is img = inital else design ||= 0 colors ||= COLORS_GENESIS_WHITE 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 ] + colors.map { |color| Pixelart::Color.parse( color ) } ## puts " colors:" ## pp colors img = ChunkyPNG::Image.new( design.width, design.height, ChunkyPNG::Color::TRANSPARENT ) # why? why not? design.each_with_index do |row, y| row.each_with_index do |color, x| if color > 0 pixel = colors[ color ] ## note: special built-in color palette black & white "hack" ## only active if colors 6 & 7 NOT defined ## color 6 => black (000000 / ff) rgb / a(lpha) ## color 7 => white (ffffff / ff) rgb / a(lpha) pixel = 0xff if pixel.nil? && color == 6 pixel = 0xffffffff if pixel.nil? && color == 7 img[x,y] = pixel end # has color? end # each row end # each data end super( img.width, img.height, img ) end |
Class Method Details
.derive_palette(r: nil, g: nil, b: nil, hue: nil, invert: false) ⇒ Object
(static) helpers
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 |
# File 'lib/mooncats/image.rb', line 103 def self.derive_palette( r: nil, g: nil, b: nil, hue: nil, invert: false ) if hue ## pass through as is 1:1 else ## assume r, g, b ## 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] hue = hsl[0] end hx = hue % 360 ## note: makes sure number is always POSITIVE (e.g. -13 % 360 => 347) hy = (hue + 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) ⇒ Object Also known as: mint
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mooncats/image.rb', line 12 def self.generate( id ) = Metadata.new( id ) design = .design.to_i # note: meta.design is a struct/object - keep/use a local int !!! colors = if .genesis? if design % 2 == 0 && .invert? || design % 2 == 1 && !.invert? COLORS_GENESIS_WHITE else COLORS_GENESIS_BLACK end else derive_palette( r: .r, g: .g, b: .b, invert: .invert? ) end new( design: design, colors: colors ) end |
.read(path) ⇒ Object
convenience helper
41 42 43 44 |
# File 'lib/mooncats/image.rb', line 41 def self.read( path ) ## convenience helper img = ChunkyPNG::Image.from_file( path ) new( img ) end |