sugar_png 0.0.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +6 -4
- data/Gemfile.lock +77 -24
- data/README.md +105 -0
- data/README.md.tpl +99 -0
- data/Rakefile +41 -6
- data/VERSION +1 -1
- data/data/1-dump-hex.rb +22 -0
- data/data/2-marshal-test.rb +22 -0
- data/data/3-hex2marshal.rb +71 -0
- data/data/font/00 +0 -0
- data/data/font/01 +0 -0
- data/data/font/02 +0 -0
- data/data/font/03 +0 -0
- data/data/font/04 +0 -0
- data/data/font/05 +0 -0
- data/data/font/06 +0 -0
- data/data/font/07 +0 -0
- data/data/font/09 +0 -0
- data/data/font/0a +0 -0
- data/data/font/0b +0 -0
- data/data/font/0c +0 -0
- data/data/font/0d +0 -0
- data/data/font/0e +0 -0
- data/data/font/0f +0 -0
- data/data/font/10 +0 -0
- data/data/font/11 +0 -0
- data/data/font/12 +0 -0
- data/data/font/13 +0 -0
- data/data/font/14 +0 -0
- data/data/font/15 +0 -0
- data/data/font/16 +0 -0
- data/data/font/17 +0 -0
- data/data/font/18 +0 -0
- data/data/font/19 +0 -0
- data/data/font/1a +0 -0
- data/data/font/1b +0 -0
- data/data/font/1c +0 -0
- data/data/font/1d +0 -0
- data/data/font/1e +0 -0
- data/data/font/1f +0 -0
- data/data/font/20 +0 -0
- data/data/font/21 +0 -0
- data/data/font/22 +0 -0
- data/data/font/23 +0 -0
- data/data/font/24 +0 -0
- data/data/font/25 +0 -0
- data/data/font/26 +0 -0
- data/data/font/27 +0 -0
- data/data/font/28 +0 -0
- data/data/font/29 +0 -0
- data/data/font/2a +0 -0
- data/data/font/2b +0 -0
- data/data/font/2c +0 -0
- data/data/font/2d +0 -0
- data/data/font/2e +0 -0
- data/data/font/2f +0 -0
- data/data/font/30 +0 -0
- data/data/font/31 +0 -0
- data/data/font/32 +0 -0
- data/data/font/33 +0 -0
- data/data/font/34 +0 -0
- data/data/font/35 +0 -0
- data/data/font/36 +0 -0
- data/data/font/37 +0 -0
- data/data/font/38 +0 -0
- data/data/font/39 +0 -0
- data/data/font/3a +0 -0
- data/data/font/3b +0 -0
- data/data/font/3c +0 -0
- data/data/font/3d +0 -0
- data/data/font/3e +0 -0
- data/data/font/3f +0 -0
- data/data/font/40 +0 -0
- data/data/font/41 +0 -0
- data/data/font/42 +0 -0
- data/data/font/43 +0 -0
- data/data/font/44 +0 -0
- data/data/font/45 +0 -0
- data/data/font/46 +0 -0
- data/data/font/47 +0 -0
- data/data/font/48 +0 -0
- data/data/font/49 +0 -0
- data/data/font/4a +0 -0
- data/data/font/4b +0 -0
- data/data/font/4c +0 -0
- data/data/font/4d +0 -0
- data/data/font/4e +0 -0
- data/data/font/4f +0 -0
- data/data/font/50 +0 -0
- data/data/font/51 +0 -0
- data/data/font/52 +0 -0
- data/data/font/53 +0 -0
- data/data/font/54 +0 -0
- data/data/font/55 +0 -0
- data/data/font/56 +0 -0
- data/data/font/57 +0 -0
- data/data/font/58 +0 -0
- data/data/font/59 +0 -0
- data/data/font/5a +0 -0
- data/data/font/5b +0 -0
- data/data/font/5c +0 -0
- data/data/font/5d +0 -0
- data/data/font/5e +0 -0
- data/data/font/5f +0 -0
- data/data/font/60 +0 -0
- data/data/font/61 +0 -0
- data/data/font/62 +0 -0
- data/data/font/63 +0 -0
- data/data/font/64 +0 -0
- data/data/font/65 +0 -0
- data/data/font/66 +0 -0
- data/data/font/67 +0 -0
- data/data/font/68 +0 -0
- data/data/font/69 +0 -0
- data/data/font/6a +0 -0
- data/data/font/6b +0 -0
- data/data/font/6c +0 -0
- data/data/font/6d +0 -0
- data/data/font/6e +0 -0
- data/data/font/6f +0 -0
- data/data/font/70 +0 -0
- data/data/font/71 +0 -0
- data/data/font/72 +0 -0
- data/data/font/73 +0 -0
- data/data/font/74 +0 -0
- data/data/font/75 +0 -0
- data/data/font/76 +0 -0
- data/data/font/77 +0 -0
- data/data/font/78 +0 -0
- data/data/font/79 +0 -0
- data/data/font/7a +0 -0
- data/data/font/7b +0 -0
- data/data/font/7c +0 -0
- data/data/font/7d +0 -0
- data/data/font/7e +0 -0
- data/data/font/7f +0 -0
- data/data/font/80 +0 -0
- data/data/font/81 +0 -0
- data/data/font/82 +0 -0
- data/data/font/83 +0 -0
- data/data/font/84 +0 -0
- data/data/font/85 +0 -0
- data/data/font/86 +0 -0
- data/data/font/87 +0 -0
- data/data/font/88 +0 -0
- data/data/font/89 +0 -0
- data/data/font/8a +0 -0
- data/data/font/8b +0 -0
- data/data/font/8c +0 -0
- data/data/font/8d +0 -0
- data/data/font/8e +0 -0
- data/data/font/8f +0 -0
- data/data/font/90 +0 -0
- data/data/font/91 +0 -0
- data/data/font/92 +0 -0
- data/data/font/93 +0 -0
- data/data/font/94 +0 -0
- data/data/font/95 +0 -0
- data/data/font/96 +0 -0
- data/data/font/97 +0 -0
- data/data/font/98 +0 -0
- data/data/font/99 +0 -0
- data/data/font/9a +0 -0
- data/data/font/9b +0 -0
- data/data/font/9c +0 -0
- data/data/font/9d +0 -0
- data/data/font/9e +0 -0
- data/data/font/9f +0 -0
- data/data/font/a0 +0 -0
- data/data/font/a1 +0 -0
- data/data/font/a2 +0 -0
- data/data/font/a3 +0 -0
- data/data/font/a4 +0 -0
- data/data/font/a5 +0 -0
- data/data/font/a6 +0 -0
- data/data/font/a7 +0 -0
- data/data/font/a8 +0 -0
- data/data/font/a9 +0 -0
- data/data/font/aa +0 -0
- data/data/font/ac +0 -0
- data/data/font/ad +0 -0
- data/data/font/ae +0 -0
- data/data/font/af +0 -0
- data/data/font/b0 +0 -0
- data/data/font/b1 +0 -0
- data/data/font/b2 +0 -0
- data/data/font/b3 +0 -0
- data/data/font/b4 +0 -0
- data/data/font/b5 +0 -0
- data/data/font/b6 +0 -0
- data/data/font/b7 +0 -0
- data/data/font/b8 +0 -0
- data/data/font/b9 +0 -0
- data/data/font/ba +0 -0
- data/data/font/bb +0 -0
- data/data/font/bc +0 -0
- data/data/font/bd +0 -0
- data/data/font/be +0 -0
- data/data/font/bf +0 -0
- data/data/font/c0 +0 -0
- data/data/font/c1 +0 -0
- data/data/font/c2 +0 -0
- data/data/font/c3 +0 -0
- data/data/font/c4 +0 -0
- data/data/font/c5 +0 -0
- data/data/font/c6 +0 -0
- data/data/font/c7 +0 -0
- data/data/font/c8 +0 -0
- data/data/font/c9 +0 -0
- data/data/font/ca +0 -0
- data/data/font/cb +0 -0
- data/data/font/cc +0 -0
- data/data/font/cd +0 -0
- data/data/font/ce +0 -0
- data/data/font/cf +0 -0
- data/data/font/d0 +0 -0
- data/data/font/d1 +0 -0
- data/data/font/d2 +0 -0
- data/data/font/d3 +0 -0
- data/data/font/d4 +0 -0
- data/data/font/d5 +0 -0
- data/data/font/d6 +0 -0
- data/data/font/d7 +0 -0
- data/data/font/f9 +0 -0
- data/data/font/fa +0 -0
- data/data/font/fb +0 -0
- data/data/font/fc +0 -0
- data/data/font/fd +0 -0
- data/data/font/fe +0 -0
- data/data/font/ff +0 -0
- data/data/unifont-5.1.20080820.7z +0 -0
- data/data/unused-4-read-tar.rb +49 -0
- data/lib/sugar_png.rb +164 -2
- data/lib/sugar_png/border.rb +20 -0
- data/lib/sugar_png/color.rb +118 -0
- data/lib/sugar_png/datastream.rb +2 -17
- data/lib/sugar_png/dyn_accessor.rb +28 -0
- data/lib/sugar_png/font.rb +44 -0
- data/lib/sugar_png/glyph.rb +36 -0
- data/lib/sugar_png/image.rb +104 -0
- data/samples/damaged_chunk.png +0 -0
- data/samples/damaged_signature.png +0 -0
- data/samples/png_suite/f00n0g08_reference.png +0 -0
- data/samples/png_suite/f00n2c08_reference.png +0 -0
- data/samples/png_suite/f01n0g08_reference.png +0 -0
- data/samples/png_suite/f01n2c08_reference.png +0 -0
- data/samples/png_suite/f02n0g08_reference.png +0 -0
- data/samples/png_suite/f02n2c08_reference.png +0 -0
- data/samples/png_suite/f03n0g08_reference.png +0 -0
- data/samples/png_suite/f03n2c08_reference.png +0 -0
- data/samples/png_suite/f04n0g08_reference.png +0 -0
- data/samples/png_suite/f04n2c08_reference.png +0 -0
- data/samples/readme/explicit_image_dimensions_bg_color.png +0 -0
- data/samples/readme/hello_world.png +0 -0
- data/samples/readme/japanese_text_with_rainbow_borders_zoomed_4x.png +0 -0
- data/samples/readme/pixels_can_be_set_using_ranges_enumerators_arrays.png +0 -0
- data/samples/readme/playing_with_transparency_16_bit_color_depth.png +0 -0
- data/samples/readme/white_noise.png +0 -0
- data/samples/rgba.tar.bz2 +0 -0
- data/samples/text_chunk.png +0 -0
- data/samples/ztxt_chunk.png +0 -0
- data/spec/color_spec.rb +81 -0
- data/spec/font_spec.rb +82 -0
- data/spec/magic/border_spec.rb +49 -0
- data/spec/magic/create_spec.rb +42 -0
- data/spec/magic/pixels_spec.rb +73 -0
- data/spec/magic/text_spec.rb +113 -0
- data/spec/magic/zoom_spec.rb +38 -0
- data/spec/png_suite_spec.rb +145 -0
- data/spec/spec_helper.rb +45 -1
- data/spec/sugar_png/datastream_spec.rb +32 -0
- data/spec/support/png_suite.rb +44 -0
- data/sugar_png.gemspec +295 -28
- metadata +331 -50
data/data/font/9d
ADDED
Binary file
|
data/data/font/9e
ADDED
Binary file
|
data/data/font/9f
ADDED
Binary file
|
data/data/font/a0
ADDED
Binary file
|
data/data/font/a1
ADDED
Binary file
|
data/data/font/a2
ADDED
Binary file
|
data/data/font/a3
ADDED
Binary file
|
data/data/font/a4
ADDED
Binary file
|
data/data/font/a5
ADDED
Binary file
|
data/data/font/a6
ADDED
Binary file
|
data/data/font/a7
ADDED
Binary file
|
data/data/font/a8
ADDED
Binary file
|
data/data/font/a9
ADDED
Binary file
|
data/data/font/aa
ADDED
Binary file
|
data/data/font/ac
ADDED
Binary file
|
data/data/font/ad
ADDED
Binary file
|
data/data/font/ae
ADDED
Binary file
|
data/data/font/af
ADDED
Binary file
|
data/data/font/b0
ADDED
Binary file
|
data/data/font/b1
ADDED
Binary file
|
data/data/font/b2
ADDED
Binary file
|
data/data/font/b3
ADDED
Binary file
|
data/data/font/b4
ADDED
Binary file
|
data/data/font/b5
ADDED
Binary file
|
data/data/font/b6
ADDED
Binary file
|
data/data/font/b7
ADDED
Binary file
|
data/data/font/b8
ADDED
Binary file
|
data/data/font/b9
ADDED
Binary file
|
data/data/font/ba
ADDED
Binary file
|
data/data/font/bb
ADDED
Binary file
|
data/data/font/bc
ADDED
Binary file
|
data/data/font/bd
ADDED
Binary file
|
data/data/font/be
ADDED
Binary file
|
data/data/font/bf
ADDED
Binary file
|
data/data/font/c0
ADDED
Binary file
|
data/data/font/c1
ADDED
Binary file
|
data/data/font/c2
ADDED
Binary file
|
data/data/font/c3
ADDED
Binary file
|
data/data/font/c4
ADDED
Binary file
|
data/data/font/c5
ADDED
Binary file
|
data/data/font/c6
ADDED
Binary file
|
data/data/font/c7
ADDED
Binary file
|
data/data/font/c8
ADDED
Binary file
|
data/data/font/c9
ADDED
Binary file
|
data/data/font/ca
ADDED
Binary file
|
data/data/font/cb
ADDED
Binary file
|
data/data/font/cc
ADDED
Binary file
|
data/data/font/cd
ADDED
Binary file
|
data/data/font/ce
ADDED
Binary file
|
data/data/font/cf
ADDED
Binary file
|
data/data/font/d0
ADDED
Binary file
|
data/data/font/d1
ADDED
Binary file
|
data/data/font/d2
ADDED
Binary file
|
data/data/font/d3
ADDED
Binary file
|
data/data/font/d4
ADDED
Binary file
|
data/data/font/d5
ADDED
Binary file
|
data/data/font/d6
ADDED
Binary file
|
data/data/font/d7
ADDED
Binary file
|
data/data/font/f9
ADDED
Binary file
|
data/data/font/fa
ADDED
Binary file
|
data/data/font/fb
ADDED
Binary file
|
data/data/font/fc
ADDED
Binary file
|
data/data/font/fd
ADDED
Binary file
|
data/data/font/fe
ADDED
Binary file
|
data/data/font/ff
ADDED
Binary file
|
Binary file
|
@@ -0,0 +1,49 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# struct tarfile_entry_posix
|
4
|
+
# { // pack/unpack
|
5
|
+
# char name[100]; // ASCII (+ Z unless filled) a100/Z100
|
6
|
+
# char mode[8]; // 0 padded, octal, null a8 /A8
|
7
|
+
# char uid[8]; // ditto a8 /A8
|
8
|
+
# char gid[8]; // ditto a8 /A8
|
9
|
+
# char size[12]; // 0 padded, octal, null a12 /A12
|
10
|
+
# char mtime[12]; // 0 padded, octal, null a12 /A12
|
11
|
+
# char checksum[8]; // 0 padded, octal, null, space a8 /A8
|
12
|
+
# char typeflag[1]; // see below a /a
|
13
|
+
# char linkname[100]; // ASCII + (Z unless filled) a100/Z100
|
14
|
+
# char magic[6]; // "ustar\0" a6 /A6
|
15
|
+
# char version[2]; // "00" a2 /A2
|
16
|
+
# char uname[32]; // ASCIIZ a32 /Z32
|
17
|
+
# char gname[32]; // ASCIIZ a32 /Z32
|
18
|
+
# char devmajor[8]; // 0 padded, octal, null a8 /A8
|
19
|
+
# char devminor[8]; // 0 padded, octal, null a8 /A8
|
20
|
+
# char prefix[155]; // ASCII (+ Z unless filled) a155/Z155
|
21
|
+
# };
|
22
|
+
|
23
|
+
HEADER_UNPACK_FORMAT = "Z100x24A12"
|
24
|
+
|
25
|
+
File.open("data.tar","rb") do |f|
|
26
|
+
while true
|
27
|
+
data = f.read 136 # 100+8+8+8+12 - we don't want all the fields from header
|
28
|
+
name,size = data.unpack(HEADER_UNPACK_FORMAT)
|
29
|
+
|
30
|
+
# "end of archive is marked by at least 2 consecutive zero-filled records"
|
31
|
+
# - but I make it simpler b/c I generate archive myself and guarantee that
|
32
|
+
# EOF is one empty entry // ZZZ
|
33
|
+
break if name.empty?
|
34
|
+
size = size.to_i(8)
|
35
|
+
|
36
|
+
printf "[.] %04x: ", f.tell+376
|
37
|
+
p [name,size]
|
38
|
+
|
39
|
+
# The file data is written unaltered except that its length is rounded up
|
40
|
+
# to a multiple of 512 bytes and the extra space is filled with zero bytes.
|
41
|
+
if size & 0x1ff != 0
|
42
|
+
size = (size | 0x1ff) + 1
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
# 376 = 512-136 - compensate for skipped header bytes
|
47
|
+
f.seek size+376, IO::SEEK_CUR
|
48
|
+
end
|
49
|
+
end
|
data/lib/sugar_png.rb
CHANGED
@@ -1,5 +1,167 @@
|
|
1
1
|
require 'zpng'
|
2
2
|
|
3
|
-
|
3
|
+
require 'sugar_png/dyn_accessor'
|
4
|
+
require 'sugar_png/color'
|
5
|
+
require 'sugar_png/border'
|
6
|
+
require 'sugar_png/image'
|
7
|
+
require 'sugar_png/font'
|
8
|
+
require 'sugar_png/glyph'
|
4
9
|
|
5
|
-
|
10
|
+
class SugarPNG
|
11
|
+
DEFAULT_BG = :transparent
|
12
|
+
DEFAULT_FG = :black
|
13
|
+
|
14
|
+
Canvas = Datastream = Image
|
15
|
+
|
16
|
+
class Exception < ::Exception; end
|
17
|
+
class ArgumentError < Exception; end
|
18
|
+
|
19
|
+
extend DynAccessor
|
20
|
+
dyn_accessor :width, :height, :zoom, :depth
|
21
|
+
dyn_accessor :bg => %w'background bg_color background_color'
|
22
|
+
dyn_accessor :fg => %w'foreground fg_color foreground_color color'
|
23
|
+
|
24
|
+
def initialize h={}, &block
|
25
|
+
@bg = DEFAULT_BG
|
26
|
+
@fg = DEFAULT_FG
|
27
|
+
@zoom = 1
|
28
|
+
clear
|
29
|
+
|
30
|
+
if block_given?
|
31
|
+
if block.arity == 1
|
32
|
+
yield self
|
33
|
+
else
|
34
|
+
instance_eval &block
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# boring non-magic hash
|
39
|
+
h.each do |k,v|
|
40
|
+
self.send "#{k}=", v
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# reset all drawings
|
45
|
+
def clear
|
46
|
+
@pixels = Hash.new{ |k,v| k[v] = {} }
|
47
|
+
@borders = []
|
48
|
+
end
|
49
|
+
|
50
|
+
# set pixels
|
51
|
+
# accepted coordinate values:
|
52
|
+
# a) boring Integers
|
53
|
+
# b) neat Arrays
|
54
|
+
# c) long Ranges
|
55
|
+
# d) super Enumerators
|
56
|
+
#
|
57
|
+
# accepted color values: see SugarPNG::Color
|
58
|
+
def []= ax, ay, color
|
59
|
+
Array(ay).each do |y|
|
60
|
+
Array(ax).each do |x|
|
61
|
+
@pixels[y][x] = color
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# same as above, but color argument can be optional
|
67
|
+
def pixel ax, ay, color = @fg
|
68
|
+
Array(ay).each do |y|
|
69
|
+
Array(ax).each do |x|
|
70
|
+
@pixels[y][x] = color
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
alias :pixels :pixel
|
75
|
+
|
76
|
+
%w'put_pixel set_pixel point dot'.each do |x|
|
77
|
+
# plural & singular aliases to increase entropy & prevent global singularity
|
78
|
+
class_eval "alias :#{x} :pixel; alias :#{x}s :pixels"
|
79
|
+
end
|
80
|
+
|
81
|
+
# draw image border with specified color
|
82
|
+
def border size, color = nil
|
83
|
+
color ||= @fg || @bg
|
84
|
+
@borders << Border.new( size.is_a?(Hash) ? size : {:size => size, :color => color} )
|
85
|
+
end
|
86
|
+
|
87
|
+
# same as border, but default color is background
|
88
|
+
def padding size, color = @bg
|
89
|
+
border size, color
|
90
|
+
end
|
91
|
+
|
92
|
+
# draw a single glyph, used from within text()
|
93
|
+
def draw_glyph glyph, x0, y, color
|
94
|
+
#TODO: optimize?
|
95
|
+
glyph.to_a.each do |row|
|
96
|
+
x = x0
|
97
|
+
row.each do |bit|
|
98
|
+
self[x,y] = color if bit == 1
|
99
|
+
x += 1
|
100
|
+
end
|
101
|
+
y += 1
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# draws text, optional arguments are :color, :x, :y
|
106
|
+
def text text, h = {}
|
107
|
+
font = @font ||= Font.new
|
108
|
+
color = h[:color] || @fg
|
109
|
+
y = h[:y] || 0
|
110
|
+
text.split(/[\r\n]+/).each do |line|
|
111
|
+
x = h[:x] || 0
|
112
|
+
line.each_char do |c|
|
113
|
+
glyph = font[c]
|
114
|
+
draw_glyph glyph, x, y, color
|
115
|
+
x += glyph.width
|
116
|
+
end
|
117
|
+
y += font.height
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# export PNG to file
|
122
|
+
def save fname
|
123
|
+
File.open(fname, "wb"){ |f| f<<to_s }
|
124
|
+
end
|
125
|
+
|
126
|
+
# get PNG as bytestream, for saving it to file manually, or for sending via HTTP
|
127
|
+
def to_s
|
128
|
+
height = @height || ((t=@pixels.keys.max) && t+1 ) || 0
|
129
|
+
width = @width || ((t=@pixels.values.map(&:keys).map(&:max).max) && t+1 ) || 0
|
130
|
+
|
131
|
+
xofs = yofs = 0
|
132
|
+
xmax = width-1
|
133
|
+
ymax = height-1
|
134
|
+
|
135
|
+
if @borders.any?
|
136
|
+
width += @borders.map(&:width).inject(&:+)
|
137
|
+
height += @borders.map(&:height).inject(&:+)
|
138
|
+
xofs += @borders.map(&:left).inject(&:+)
|
139
|
+
yofs += @borders.map(&:top).inject(&:+)
|
140
|
+
end
|
141
|
+
|
142
|
+
raise(Exception.new("invalid image height #{height}")) if height <= 0
|
143
|
+
raise(Exception.new("invalid image width #{width}")) if width <= 0
|
144
|
+
|
145
|
+
img = Image.new :width => width, :height => height, :depth => @depth
|
146
|
+
img.clear(_color(@bg)) if @bg
|
147
|
+
img.draw_borders(@borders.each{ |b| b.color = _color(b.color)} )
|
148
|
+
|
149
|
+
@pixels.each do |y, xh|
|
150
|
+
next if y>ymax
|
151
|
+
xh.each do |x, c|
|
152
|
+
next if x>xmax
|
153
|
+
img[x+xofs,y+yofs] = _color(c)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
img.zoom(@zoom).export
|
158
|
+
end
|
159
|
+
alias :export :to_s
|
160
|
+
|
161
|
+
private
|
162
|
+
|
163
|
+
# create color from any of the supported color representations
|
164
|
+
def _color c
|
165
|
+
c = c.is_a?(ZPNG::Color) ? c : Color.new(c, :depth => @depth)
|
166
|
+
end
|
167
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class SugarPNG
|
2
|
+
class Border
|
3
|
+
attr_accessor :left, :right, :top, :bottom, :color
|
4
|
+
def initialize h
|
5
|
+
@color = h[:color] || raise(ArgumentError.new("border color must be set"))
|
6
|
+
@left = (h[:left] || h[:size]).to_i
|
7
|
+
@right = (h[:right] || h[:size]).to_i
|
8
|
+
@top = (h[:top] || h[:size]).to_i
|
9
|
+
@bottom= (h[:bottom]|| h[:size]).to_i
|
10
|
+
end
|
11
|
+
|
12
|
+
def width
|
13
|
+
@left + @right
|
14
|
+
end
|
15
|
+
|
16
|
+
def height
|
17
|
+
@top + @bottom
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
class SugarPNG
|
2
|
+
class Color < ZPNG::Color
|
3
|
+
class << self
|
4
|
+
def a color
|
5
|
+
color.a || (2**color.depth-1)
|
6
|
+
end
|
7
|
+
|
8
|
+
def r color; color.r; end
|
9
|
+
def g color; color.g; end
|
10
|
+
def b color; color.b; end
|
11
|
+
end
|
12
|
+
|
13
|
+
# accepted color values:
|
14
|
+
# a) Strings: "blue", "RED", ...
|
15
|
+
# b) Symbols: :blue, :red, ...
|
16
|
+
# c) HTML notation: #cc3344, #ccc, ...
|
17
|
+
# d) 3..4 int args: (10, 20, 30) - RGB; (0x20,0x30,0x40,0xff) - RGBA
|
18
|
+
# e) Hexadecimals 0xaabbcc - RGB; 0x80aabbcc - RGBA
|
19
|
+
# f) Hashes (long): { :red => 10, :green => 30, :blue => 40, :alpha => 0x80 }
|
20
|
+
# g) Hashes (shrt): { r:10, g:20, b:30, a:50 } (alpha is optional)
|
21
|
+
#
|
22
|
+
# all notations also accept one optional last argument - hash:
|
23
|
+
# :depth => 1..16 - color depth of each channel, including alpha
|
24
|
+
# :alpha => 0..2^depth - explicit definition of alpha value
|
25
|
+
def initialize *args
|
26
|
+
h = args.last.is_a?(Hash) ? args.pop : {}
|
27
|
+
|
28
|
+
@depth = h.delete(:depth) || 8
|
29
|
+
raise ArgumentError.new "invalid depth: #@depth" unless (1..16).include?(@depth)
|
30
|
+
|
31
|
+
max = 2**@depth-1
|
32
|
+
|
33
|
+
case args.size
|
34
|
+
when 0 # single Hash that already in h
|
35
|
+
# init colors to default values, we'll assign them later
|
36
|
+
@r = @g = @b = 0
|
37
|
+
when 1 # String(name or html), Symbol, Array, Integer, Hash
|
38
|
+
case arg = args.first
|
39
|
+
when String
|
40
|
+
_from_string(arg)
|
41
|
+
|
42
|
+
when Symbol
|
43
|
+
_from_string(arg.to_s)
|
44
|
+
|
45
|
+
when Array
|
46
|
+
case arg.size
|
47
|
+
when 3
|
48
|
+
@r,@g,@b = arg
|
49
|
+
when 4
|
50
|
+
@r,@g,@b,@a = arg
|
51
|
+
else
|
52
|
+
raise ArgumentError.new "invalid array size: #{arg.size}, must be 3 or 4"
|
53
|
+
end
|
54
|
+
when Integer
|
55
|
+
@r = (arg >> (@depth*2)) & max
|
56
|
+
@g = (arg >> (@depth)) & max
|
57
|
+
@b = arg & max
|
58
|
+
when Hash
|
59
|
+
# init colors to default values, we'll assign them later
|
60
|
+
@r = @g = @b = 0
|
61
|
+
h.merge! args.first
|
62
|
+
else
|
63
|
+
raise ArgumentError.new "invalid argument type: #{args.first.class}"
|
64
|
+
end
|
65
|
+
when 3 # r, g, b
|
66
|
+
@r, @g, @b = args
|
67
|
+
when 4 # r, g, b, a
|
68
|
+
@r, @g, @b, @a = args
|
69
|
+
else
|
70
|
+
raise ArgumentError.new "invalid number of arguments: #{args.size}"
|
71
|
+
end
|
72
|
+
|
73
|
+
@a ||= max # opaque by default
|
74
|
+
|
75
|
+
h.each do |k,v|
|
76
|
+
case k
|
77
|
+
when :r, :red
|
78
|
+
@r = v
|
79
|
+
when :g, :green
|
80
|
+
@g = v
|
81
|
+
when :b, :blue
|
82
|
+
@b = v
|
83
|
+
when :a, :alpha
|
84
|
+
@a = v
|
85
|
+
else
|
86
|
+
raise ArgumentError.new "invalid key: #{k}"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
[:r, :g, :b, :a].each do |x|
|
91
|
+
v = self.send(x).to_i
|
92
|
+
raise ArgumentError.new "invalid channel value: #{v}" if v<0 || v>max
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
# initialize self from string, either HTML color or simple color name
|
99
|
+
# @depth must already be set!
|
100
|
+
def _from_string s
|
101
|
+
if s[0,1] == "#"
|
102
|
+
# html colors #aabbcc or #abc
|
103
|
+
case s.size
|
104
|
+
when 4
|
105
|
+
@r,@g,@b = s[1..-1].split('').map{ |x| x.to_i(16)*17 }
|
106
|
+
when 7
|
107
|
+
@r,@g,@b = s[1..-1].scan(/../).map{ |x| x.to_i(16) }
|
108
|
+
else
|
109
|
+
raise ArgumentError.new "invalid HTML color #{s}"
|
110
|
+
end
|
111
|
+
else
|
112
|
+
@r, @g, @b, @a = self.class.const_get(s.strip.upcase).to_depth(@depth).to_a
|
113
|
+
end
|
114
|
+
rescue
|
115
|
+
raise ArgumentError.new "invalid color name: #{s}"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|