pixelart 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Manifest.txt +1 -0
- data/lib/pixelart/base.rb +3 -0
- data/lib/pixelart/composite.rb +77 -0
- data/lib/pixelart/image.rb +3 -0
- data/lib/pixelart/pixelator.rb +52 -4
- data/lib/pixelart/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 066a13d4aff234657b8685f611168ed012fd6b949a292add28e7729f31c1bdc7
|
4
|
+
data.tar.gz: a7f748b8c246bb4c944da848baef19d4fdc6cd9c74ac96050c38aa907e680247
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de3a072fd7dd9fe939340f1914fdeab6cc16dd5a2c25fb86548a3d5d89eb043b8d236dd62e2be86ac191bc0a5689eec2d3d6a3b465e08853a85cc285f394701a
|
7
|
+
data.tar.gz: 6fc5ffec090a5fbdc29b990812168d7716b4a563f3faffe8bd69ed09da70ca079af6334fa523cfdf3ed96f1228fd35842bc44d72139eeb6dc1b1d6299bf08b09
|
data/Manifest.txt
CHANGED
data/lib/pixelart/base.rb
CHANGED
@@ -14,6 +14,7 @@ require 'pixelart/color'
|
|
14
14
|
require 'pixelart/gradient'
|
15
15
|
require 'pixelart/palette'
|
16
16
|
require 'pixelart/image'
|
17
|
+
require 'pixelart/composite'
|
17
18
|
|
18
19
|
require 'pixelart/pixelator'
|
19
20
|
|
@@ -33,6 +34,8 @@ module Pixelart
|
|
33
34
|
|
34
35
|
Palette256Image = Palette8BitImage = Palette8bitImage =
|
35
36
|
ImagePalette256 = ImagePalette8Bit = ImagePalette8bit
|
37
|
+
|
38
|
+
CompositeImage = ImageComposite
|
36
39
|
end
|
37
40
|
|
38
41
|
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Pixelart
|
2
|
+
|
3
|
+
class ImageComposite < Image # check: (re)name to Collage, Sheet, Sprites, or such?
|
4
|
+
|
5
|
+
## default tile width / height in pixel -- check: (re)name to sprite or such? why? why not?
|
6
|
+
TILE_WIDTH = 24
|
7
|
+
TILE_HEIGHT = 24
|
8
|
+
|
9
|
+
|
10
|
+
def initialize( *args, **kwargs )
|
11
|
+
@tile_width = kwargs[:width] || kwargs[:tile_width] || TILE_WIDTH
|
12
|
+
@tile_height = kwargs[:height] || kwargs[:tile_height] || TILE_HEIGHT
|
13
|
+
|
14
|
+
## todo/fix: check type - args[0] is Image!!!
|
15
|
+
if args.size == 1 ## assume "copy" c'tor with passed in image
|
16
|
+
img = args[0] ## pass image through as-is
|
17
|
+
|
18
|
+
@tile_cols = img.width / @tile_width ## e.g. 2400/24 = 100
|
19
|
+
@tile_rows = img.height / @tile_height ## e.g. 2400/24 = 100
|
20
|
+
@tile_count = @tile_cols * @tile_rows ## ## 10000 = 100x100 (2400x2400 pixel)
|
21
|
+
elsif args.size == 2 || args.size == 0 ## cols, rows
|
22
|
+
## todo/fix: check type - args[0] & args[1] is Integer!!!!!
|
23
|
+
## todo/check - find a better name for cols/rows - why? why not?
|
24
|
+
@tile_cols = args[0] || 3
|
25
|
+
@tile_rows = args[1] || 3
|
26
|
+
@tile_count = 0 # (track) current index (of added images)
|
27
|
+
|
28
|
+
img = ChunkyPNG::Image.new( @tile_cols * @tile_width,
|
29
|
+
@tile_rows * @tile_height )
|
30
|
+
else
|
31
|
+
raise ArgumentError, "cols, rows or image arguments expected; got: #{args.inspect}"
|
32
|
+
end
|
33
|
+
|
34
|
+
puts " #{img.height}x#{img.width} (height x width)"
|
35
|
+
|
36
|
+
super( nil, nil, img )
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
def count() @tile_count; end
|
41
|
+
alias_method :size, :count ## add size alias (confusing if starting with 0?) - why? why not?
|
42
|
+
|
43
|
+
#####
|
44
|
+
# set / add tile
|
45
|
+
|
46
|
+
def add( image )
|
47
|
+
y, x = @tile_count.divmod( @tile_cols )
|
48
|
+
|
49
|
+
puts " [#{@tile_count}] @ (#{x}/#{y}) #{image.width}x#{image.height} (height x width)"
|
50
|
+
|
51
|
+
## note: image.image - "unwrap" the "raw" ChunkyPNG::Image
|
52
|
+
@img.compose!( image.image, x*@tile_width, y*@tile_height )
|
53
|
+
@tile_count += 1
|
54
|
+
end
|
55
|
+
alias_method :<<, :add
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
######
|
60
|
+
# get tile
|
61
|
+
|
62
|
+
def tile( index )
|
63
|
+
y, x = index.divmod( @tile_rows )
|
64
|
+
img = @img.crop( x*@tile_width, y*@tile_height, @tile_width, @tile_height )
|
65
|
+
Image.new( img.width, img.height, img ) ## wrap in pixelart image
|
66
|
+
end
|
67
|
+
|
68
|
+
def []( *args ) ## overload - why? why not?
|
69
|
+
if args.size == 1
|
70
|
+
index = args[0]
|
71
|
+
tile( index )
|
72
|
+
else
|
73
|
+
super ## e.g [x,y] --- get pixel
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end # class ImageComposite
|
77
|
+
end # module Pixelart
|
data/lib/pixelart/image.rb
CHANGED
data/lib/pixelart/pixelator.rb
CHANGED
@@ -76,7 +76,7 @@ class Pixelator # or use Minifier or such - rename - why? why not?
|
|
76
76
|
alias_method :[], :pixel
|
77
77
|
|
78
78
|
|
79
|
-
def can_pixelate?
|
79
|
+
def can_pixelate?( threshold: 50 )
|
80
80
|
# check if any pixel has NOT a color with a 50% majority?
|
81
81
|
count = 0
|
82
82
|
@width.times do |x|
|
@@ -84,10 +84,13 @@ class Pixelator # or use Minifier or such - rename - why? why not?
|
|
84
84
|
pixel = pixel( x, y )
|
85
85
|
sum = pixel.values.sum
|
86
86
|
color_count = pixel.values[0]
|
87
|
-
|
87
|
+
|
88
|
+
threshold_count = sum / (100/threshold)
|
89
|
+
if color_count < threshold_count
|
88
90
|
count += 1
|
91
|
+
puts "!! #{color_count} < #{threshold_count} (#{threshold}%)"
|
89
92
|
## todo/check: stor warn in a public errors or warns array - why? why not?
|
90
|
-
puts "!! WARN #{count} - pixel (#{x}/#{y}) - no majority (
|
93
|
+
puts "!! WARN #{count} - pixel (#{x}/#{y}) - no majority (#{threshold}%) color:"
|
91
94
|
pp pixel
|
92
95
|
end
|
93
96
|
end
|
@@ -111,7 +114,52 @@ class Pixelator # or use Minifier or such - rename - why? why not?
|
|
111
114
|
|
112
115
|
Image.new( img.width, img.height, img ) ## wrap in Pixelart::Image - why? why not?
|
113
116
|
end
|
117
|
+
|
118
|
+
def outline
|
119
|
+
## create a two color outline (transparent and non-transparent color)
|
120
|
+
img = ChunkyPNG::Image.new( @width, @height )
|
121
|
+
|
122
|
+
@width.times do |x|
|
123
|
+
@height.times do |y|
|
124
|
+
pixel = pixel( x, y )
|
125
|
+
## calculate pixel count for transparent and non-transparent parts
|
126
|
+
## note:
|
127
|
+
## also count all colors with alpha channel < 200 to transparent!!
|
128
|
+
transparent_count, color_count = pixel.reduce([0,0]) do |mem, (color,count)|
|
129
|
+
hsl = Color.to_hsl( color )
|
130
|
+
## get alpha channel (transparency) for hsla
|
131
|
+
## 0-255 max.
|
132
|
+
alpha = hsl[3]
|
133
|
+
if color == 0x00 || alpha < 200
|
134
|
+
mem[0] += count
|
135
|
+
else
|
136
|
+
mem[1] += count
|
137
|
+
end
|
138
|
+
mem
|
139
|
+
end
|
140
|
+
|
141
|
+
print "."
|
142
|
+
if transparent_count > 0 && color_count > 0
|
143
|
+
print "(#{x}/#{y}=>#{transparent_count}/#{color_count})"
|
144
|
+
end
|
145
|
+
|
146
|
+
## todo/check:
|
147
|
+
## warn if sum_transparent == sum_color
|
148
|
+
## or within "threshold" e.g. below 55% or 58% or such - why? why not?
|
149
|
+
## or add treshold as param to outline?
|
150
|
+
color = if transparent_count > color_count
|
151
|
+
0x0
|
152
|
+
else
|
153
|
+
0x0000ffff ## use blue for now
|
154
|
+
end
|
155
|
+
|
156
|
+
img[x,y] = color
|
157
|
+
end
|
158
|
+
end
|
159
|
+
print "\n"
|
160
|
+
|
161
|
+
Image.new( img.width, img.height, img ) ## wrap in Pixelart::Image - why? why not?
|
162
|
+
end
|
114
163
|
end # class Pixelator
|
115
164
|
end # module Pixelart
|
116
165
|
|
117
|
-
|
data/lib/pixelart/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pixelart
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-05-
|
11
|
+
date: 2021-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chunky_png
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- lib/pixelart.rb
|
76
76
|
- lib/pixelart/base.rb
|
77
77
|
- lib/pixelart/color.rb
|
78
|
+
- lib/pixelart/composite.rb
|
78
79
|
- lib/pixelart/gradient.rb
|
79
80
|
- lib/pixelart/image.rb
|
80
81
|
- lib/pixelart/led.rb
|