pixelart 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 71ac76ef187b8148a547f8dd7b76f2216bdd034f2ba67f7974b6e1a98abe3992
4
- data.tar.gz: 6b44348938795087874a80fec20a6e1f87633ff282f62804605b42cbb232e46d
3
+ metadata.gz: 5cc4a5d1c756256a1162bdab5208ff01549b7e2543ccb301457845dd1f393b48
4
+ data.tar.gz: 1f07a5a8203fc4b4fc93446bc52d6abf8ca1b94368c48952649c423ef0100b15
5
5
  SHA512:
6
- metadata.gz: d94abebcda0b1d87bdc0bcdc0f6072d7ae88efed8b26a0733f4fcdccb9b8063068b9a55f24bf08ebe34705e30298ebaf5d1f619045e37fe5be3b2412a8019932
7
- data.tar.gz: 63be655c80cf3703499aae7984a901888ca2ae3fe362ea66f5324e7abd98a0b207f5a150da9f2f1c6b7b2c29d52b71e44d252b6323eb8050160bac8c52e542f0
6
+ metadata.gz: 6031f337310c6935841ae3a9c2528f4aa6179c8a5b6b9a4b4aa21d0f98b9a9dd5a0578792cce660be1f15c11d95e35785efa80547e1950d28cf8e3ff21a05243
7
+ data.tar.gz: 4615099b098378c2eee14cb854c2ee67237a278172b3c789be1de72a59c9ba6779e8dc2a4e9a4384c93b2771d5c8f31a8cf1f51cff308d6bf0e27468a50d0cb3
data/Manifest.txt CHANGED
@@ -3,5 +3,7 @@ Manifest.txt
3
3
  README.md
4
4
  Rakefile
5
5
  lib/pixelart.rb
6
+ lib/pixelart/color.rb
7
+ lib/pixelart/gradient.rb
6
8
  lib/pixelart/image.rb
7
9
  lib/pixelart/version.rb
data/lib/pixelart.rb CHANGED
@@ -10,6 +10,8 @@ require 'fileutils'
10
10
 
11
11
  ## our own code
12
12
  require 'pixelart/version' # note: let version always go first
13
+ require 'pixelart/color'
14
+ require 'pixelart/gradient'
13
15
  require 'pixelart/image'
14
16
 
15
17
 
@@ -0,0 +1,62 @@
1
+ module Pixelart
2
+
3
+
4
+ class Color
5
+ TRANSPARENT = 0 # rgba( 0, 0, 0, 0)
6
+ BLACK = 0xff # rgba( 0, 0, 0,255)
7
+ WHITE = 0xffffffff # rgba(255,255,255,255)
8
+
9
+
10
+ def self.parse( color )
11
+ if color.is_a?( Integer ) ## e.g. assumes ChunkyPNG::Color.rgb() or such
12
+ color ## pass through as is 1:1
13
+ elsif color.is_a?( Array ) ## assume array of hsl(a) e. g. [180, 0.86, 0.88]
14
+ from_hsl( *color )
15
+ elsif color.is_a?( String )
16
+ if color.downcase == 'transparent' ## special case for builtin colors
17
+ TRANSPARENT
18
+ else
19
+ ## note: return an Integer !!! (not a Color class or such!!! )
20
+ from_hex( color )
21
+ end
22
+ else
23
+ raise ArgumentError, "unknown color format; cannot parse - expected rgb hex string e.g. d3d3d3"
24
+ end
25
+ end
26
+
27
+ def self.from_hex( hex )
28
+ ## Creates a color by converting it from a string in hex notation.
29
+ ##
30
+ ## It supports colors with (#rrggbbaa) or without (#rrggbb)
31
+ ## alpha channel as well as the 3-digit short format (#rgb)
32
+ ## for those without. Color strings may include
33
+ ## the prefix "0x" or "#"".
34
+ ChunkyPNG::Color.from_hex( hex )
35
+ end
36
+
37
+ def self.from_hsl( hue, saturation, lightness, alpha=255)
38
+ ChunkyPNG::Color.from_hsl( hue,
39
+ saturation,
40
+ lightness,
41
+ alpha )
42
+ end
43
+
44
+
45
+ def self.to_hex( color, include_alpha: true )
46
+ ChunkyPNG::Color.to_hex( color, include_alpha )
47
+ end
48
+
49
+ def self.to_hsl( color, include_alpha: true )
50
+ # Returns an array with the separate HSL components of a color.
51
+ ChunkyPNG::Color.to_hsl( color, include_alpha )
52
+ end
53
+
54
+ def self.r( color ) ChunkyPNG::Color.r( color ); end
55
+ def self.g( color ) ChunkyPNG::Color.g( color ); end
56
+ def self.b( color ) ChunkyPNG::Color.b( color ); end
57
+
58
+ def self.rgb( r, g, b ) ChunkyPNG::Color.rgb( r, g, b); end
59
+ end # class Color
60
+ end # module Pixelart
61
+
62
+
@@ -0,0 +1,96 @@
1
+
2
+ ## inspired by
3
+ ## https://en.wikipedia.org/wiki/List_of_software_palettes#Color_gradient_palettes
4
+ ## https://github.com/mistic100/tinygradient
5
+ ## https://mistic100.github.io/tinygradient/
6
+
7
+
8
+ module Pixelart
9
+
10
+ class Gradient
11
+
12
+ def initialize( *stops )
13
+ ## note: convert stop colors to rgb triplets e.g.
14
+ ## from #ffffff to [255,255,255]
15
+ ## #000000 to [0,0,0] etc.
16
+ @stops = stops.map do |stop|
17
+ stop = Color.parse( stop )
18
+ [Color.r(stop), Color.g(stop), Color.b(stop)]
19
+ end
20
+ end
21
+
22
+
23
+ def colors( steps )
24
+ ## note: for now only support two colors
25
+ ## note: gradient will include start (first)
26
+ ## AND stop color (last) - stop color is NOT excluded for now!!
27
+ start = @stops[0]
28
+ stop = @stops[1]
29
+
30
+ step = stepize( start, stop, steps-1 )
31
+ ## pp step
32
+
33
+ gradient = [start]
34
+
35
+ 1.upto(steps-2).each do |i|
36
+ color = interpolate( step, start, i )
37
+ gradient << color
38
+ end
39
+ ## note: use original passed in stop color (should match calculated)
40
+ gradient << stop
41
+
42
+ ## convert to color (Integer)
43
+ gradient.map do |color|
44
+ Color.rgb( *color )
45
+ end
46
+ end
47
+
48
+ ##
49
+ # Linearly compute the step size between start and end
50
+ ## (not normalized)
51
+ # @param {StepValue} start
52
+ # @param {StepValue} end
53
+ # @param {number} steps - number of desired steps
54
+ #@return {StepValue}
55
+
56
+ def stepize(start, stop, steps)
57
+ step = []
58
+
59
+ [0,1,2].each do |k|
60
+ step << Float(stop[k] - start[k]) / Float(steps)
61
+ end
62
+
63
+ step
64
+ end
65
+
66
+ ##
67
+ # Compute the final step color
68
+ # @param {StepValue} step - from `stepize`
69
+ # @param {StepValue} start
70
+ # @param {number} i - color index
71
+ # @return {StepValue}
72
+
73
+ RGB_MAX = [256, 256, 256]
74
+ def interpolate( step, start, i )
75
+ color = []
76
+
77
+ [0,1,2].each do |k|
78
+ color[k] = step[k]*i + start[k]
79
+
80
+ color[k] = if color[k] < 0.0
81
+ color[k] + RGB_MAX[k]
82
+ else
83
+ color[k] % RGB_MAX[k]
84
+ end
85
+
86
+ ## convert back to Integer from Float
87
+ ## add 0.5 for rounding up (starting with 0.5) - why? why not?
88
+ color[k] = (color[k]+0.5).to_i
89
+ end
90
+ color
91
+ end
92
+
93
+
94
+ end # class Gradient
95
+ end # module Pixelart
96
+
@@ -1,57 +1,5 @@
1
1
  module Pixelart
2
2
 
3
-
4
-
5
-
6
- class Color
7
- def self.parse( color )
8
- if color.is_a?( Integer ) ## e.g. assumes ChunkyPNG::Color.rgb() or such
9
- color ## pass through as is 1:1
10
- elsif color.is_a?( Array ) ## assume array of hsl(a) e. g. [180, 0.86, 0.88]
11
- ChunkyPNG::Color.from_hsl( *color )
12
- elsif color.is_a?( String )
13
- if color.downcase == 'transparent' ## special case for builtin colors
14
- ChunkyPNG::Color::TRANSPARENT
15
- else
16
- ## note: return an Integer !!! (not a Color class or such!!! )
17
- ChunkyPNG::Color.from_hex( color )
18
- end
19
- else
20
- raise ArgumentError, "unknown color format; cannot parse - expected rgb hex string e.g. d3d3d3"
21
- end
22
- end
23
-
24
- def self.from_hex( hex )
25
- ## Creates a color by converting it from a string in hex notation.
26
- ##
27
- ## It supports colors with (#rrggbbaa) or without (#rrggbb)
28
- ## alpha channel as well as the 3-digit short format (#rgb)
29
- ## for those without. Color strings may include
30
- ## the prefix "0x" or "#"".
31
- ChunkyPNG::Color.from_hex( hex )
32
- end
33
-
34
- def self.from_hsl( hue, saturation, lightness, alpha=255)
35
- ChunkyPNG::Color.from_hsl( hue,
36
- saturation,
37
- lightness,
38
- alpha )
39
- end
40
-
41
-
42
- def self.to_hex( color, include_alpha: true )
43
- ChunkyPNG::Color.to_hex( color, include_alpha )
44
- end
45
-
46
- def self.to_hsl( color, include_alpha: true )
47
- # Returns an array with the separate HSL components of a color.
48
- ChunkyPNG::Color.to_hsl( color, include_alpha )
49
- end
50
- end # class Color
51
-
52
-
53
-
54
-
55
3
  class Image
56
4
 
57
5
  def self.read( path ) ## convenience helper
@@ -82,7 +30,7 @@ end
82
30
 
83
31
 
84
32
 
85
- def initialize( width, height, initial=ChunkyPNG::Color::TRANSPARENT )
33
+ def initialize( width, height, initial=Color::TRANSPARENT )
86
34
 
87
35
  if initial.is_a?( ChunkyPNG::Image )
88
36
  @img = initial
@@ -204,7 +152,7 @@ end
204
152
  def self.parse_colors( colors )
205
153
  if colors.is_a?( Array ) ## convenience shortcut
206
154
  ## note: always auto-add color 0 as pre-defined transparent - why? why not?
207
- h = { '0' => ChunkyPNG::Color::TRANSPARENT }
155
+ h = { '0' => Color::TRANSPARENT }
208
156
  colors.each_with_index do |color, i|
209
157
  h[ (i+1).to_s ] = Color.parse( color )
210
158
  end
@@ -3,7 +3,7 @@ module Pixelart
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- PATCH = 2
6
+ PATCH = 3
7
7
  VERSION = [MAJOR,MINOR,PATCH].join('.')
8
8
 
9
9
  def self.version
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.1.2
4
+ version: 0.1.3
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-04-13 00:00:00.000000000 Z
11
+ date: 2021-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chunky_png
@@ -73,6 +73,8 @@ files:
73
73
  - README.md
74
74
  - Rakefile
75
75
  - lib/pixelart.rb
76
+ - lib/pixelart/color.rb
77
+ - lib/pixelart/gradient.rb
76
78
  - lib/pixelart/image.rb
77
79
  - lib/pixelart/version.rb
78
80
  homepage: https://github.com/cryptocopycats/mooncats