savio 0.1.3 → 0.1.6

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.
data/lib/savio/Scene.rb CHANGED
@@ -1,55 +1,55 @@
1
- module Savio
2
- class Scene
3
- attr_accessor :elements
4
- def initialize(file, color = 'black')
5
- @file = file
6
- @color = color
7
- show()
8
- end
9
-
10
- def show()
11
- @bg = Square.new(x: 0, y: 0, z: 999, size: Window.width+1, color: @color)
12
- @elements = []
13
- content = File.read(@file)
14
- content = content.gsub('[', "").gsub(']', "").gsub('"',"").chomp.split(",")
15
-
16
- (content.count-1).times do |i|
17
- if (i % 2) == 0
18
- classType = Object.const_get(content[i].gsub(" ",""))
19
- hash = content[i+1].gsub(';', ',').gsub('"',"").chomp
20
- pairs = hash.split(",").to_s.gsub('"',"").chomp
21
- keypair = pairs.split("=>").to_s.gsub('"',"").chomp
22
- new = keypair.gsub('{', "").gsub('}', "").gsub('[', "").gsub(']', "").gsub('"',"").gsub(" ","").chomp.split(",")
23
- build = {}
24
- (new.count-1).times do |j|
25
- if (j % 2) == 0
26
- case Savio.guessType(new[j+1].chomp)
27
- when "int"
28
- value = new[j+1].chomp.to_i
29
- when "float"
30
- value = new[j+1].chomp.to_f
31
- when "str"
32
- value = new[j+1].chomp.to_s
33
- when "bool"
34
- value = Savio.makeBool(new[j+1].chomp)
35
- end
36
- build[new[j].intern] = value
37
- end
38
- end
39
- puts "SCENE: " + @file.to_s + build.to_s
40
- @elements.push(classType.new(build))
41
- end
42
- end
43
- @elements.each do |e|
44
- e.z = 1000
45
- end
46
- end
47
-
48
- def remove()
49
- @bg.remove
50
- @elements.each do |e|
51
- e.kill()
52
- end
53
- end
54
- end
55
- end
1
+ module Savio
2
+ class Scene
3
+ attr_accessor :elements
4
+ def initialize(file, color = 'black')
5
+ @file = file
6
+ @color = color
7
+ show()
8
+ end
9
+
10
+ def show()
11
+ @bg = Square.new(x: 0, y: 0, z: 999, size: Window.width+1, color: @color)
12
+ @elements = []
13
+ content = File.read(@file)
14
+ content = content.gsub('[', "").gsub(']', "").gsub('"',"").chomp.split(",")
15
+
16
+ (content.count-1).times do |i|
17
+ if (i % 2) == 0
18
+ classType = Object.const_get(content[i].gsub(" ",""))
19
+ hash = content[i+1].gsub(';', ',').gsub('"',"").chomp
20
+ pairs = hash.split(",").to_s.gsub('"',"").chomp
21
+ keypair = pairs.split("=>").to_s.gsub('"',"").chomp
22
+ new = keypair.gsub('{', "").gsub('}', "").gsub('[', "").gsub(']', "").gsub('"',"").gsub(" ","").chomp.split(",")
23
+ build = {}
24
+ (new.count-1).times do |j|
25
+ if (j % 2) == 0
26
+ case Savio.guessType(new[j+1].chomp)
27
+ when "int"
28
+ value = new[j+1].chomp.to_i
29
+ when "float"
30
+ value = new[j+1].chomp.to_f
31
+ when "str"
32
+ value = new[j+1].chomp.to_s
33
+ when "bool"
34
+ value = Savio.makeBool(new[j+1].chomp)
35
+ end
36
+ build[new[j].intern] = value
37
+ end
38
+ end
39
+ puts "SCENE: " + @file.to_s + build.to_s
40
+ @elements.push(classType.new(build))
41
+ end
42
+ end
43
+ @elements.each do |e|
44
+ e.z = 1000
45
+ end
46
+ end
47
+
48
+ def remove()
49
+ @bg.remove
50
+ @elements.each do |e|
51
+ e.kill()
52
+ end
53
+ end
54
+ end
55
+ end
data/lib/savio/Slider.rb CHANGED
@@ -1,188 +1,195 @@
1
- module Savio
2
- class Slider
3
- include IORenderable
4
-
5
- attr_reader :length, :min, :max, :size, :value
6
-
7
- @@sliders = []
8
- def self.sliders
9
- @@sliders
10
- end
11
-
12
- def initialize(args = {})
13
- @@sliders.push(self)
14
- super(args)
15
-
16
- @length = args[:length] || 100
17
- @min = args[:min] || 0
18
- @max = args[:max] || 100
19
-
20
- @style = args[:style] || 'knob'
21
- if @style != 'knob' && @style != 'fill'
22
- @style = 'knob'
23
- end
24
-
25
- @value = args[:value] || rand(@min..@max)
26
- @showValue = args[:showValue] || true
27
-
28
- @labelColor = args[:labelColor] || '#F5F5F5'
29
- @sliderColor = args[:sliderColor] || '#757575'
30
- @knobColor = args[:knobColor] || '#5BB36A'
31
-
32
- build()
33
- end
34
-
35
- def style=(style)
36
- if style == 'knob' || style == 'fill'
37
- @style = style
38
- rebuild()
39
- end
40
- end
41
- def length=(length)
42
- @length = length.clamp(1, Window.width-@x)
43
- rebuild()
44
- end
45
- def showValue=(state)
46
- @showValue = state
47
- rebuild()
48
- end
49
- def labelColor=(c)
50
- @labelColor = c
51
- rebuild()
52
- end
53
- def sliderColor=(c)
54
- @sliderColor = c
55
- rebuild()
56
- end
57
- def knobColor=(c)
58
- @knobColor = c
59
- rebuild()
60
- end
61
- def min=(x)
62
- @min = x
63
- setValue(@value)
64
- end
65
- def max=(x)
66
- @max = x
67
- setValue(@value)
68
- end
69
-
70
- def renderKnobTo(x)
71
- if x.between?(@x, @x+@length)
72
- case @style
73
- when 'knob'
74
- @knob.x = x
75
- when 'fill'
76
- @knob.width = x - @x
77
- end
78
- end
79
- end
80
-
81
- def moveKnob(x)
82
- if x.between?(@x, @x+@length)
83
- renderKnobTo(x)
84
-
85
- to_max = @max
86
- to_min = @min
87
- from_max = @x + @length
88
- from_min = @x
89
- pos = x
90
- @value = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min)
91
- if @showValue == true
92
- @label.text = @value.round(2).to_s
93
- end
94
- end
95
- end
96
-
97
- def value=(value)
98
- setValue(value)
99
- end
100
-
101
- def setValue(value)
102
- if value.between?(@min, @max)
103
- to_max = @x + @length
104
- to_min = @x
105
- from_max = @max.to_f
106
- from_min = @min.to_f
107
- pos = value
108
- knobX = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min)
109
- @value = value
110
- if @showValue == true
111
- @label.text = @value.round(2).to_s
112
- end
113
- renderKnobTo(knobX)
114
- else
115
- setValue(@min)
116
- end
117
- end
118
-
119
- def remove()
120
- super()
121
- @sliderLine.remove
122
- @knob.remove
123
- @label.remove
124
- @nameLabel.remove
125
- end
126
-
127
- def add()
128
- super()
129
- @sliderLine.add
130
- @knob.add
131
- @label.add
132
- @nameLabel.add
133
- end
134
-
135
- def build()
136
- @shown = true
137
-
138
- @sliderLine = Line.new(
139
- x1: @x, y1: @y,
140
- x2: @x+@length, y2: @y,
141
- width: @size,
142
- color: @sliderColor,
143
- z: @z
144
- )
145
-
146
- case @style
147
- when 'knob'
148
- @knob = Circle.new(
149
- x: @x, y: @y,
150
- radius: @size * 1.2,
151
- color: @knobColor,
152
- z: @z+1
153
- )
154
- when 'fill'
155
- @knob = Rectangle.new(
156
- x: @x, y: @y,
157
- width: 0 , height: @size,
158
- color: @knobColor,
159
- z: @z+1
160
- )
161
- end
162
-
163
- @label = Text.new(
164
- @value.to_s,
165
- x: @x + @length + @size, y: @y - @size * 1.75,
166
- size: @size * 2.5,
167
- color: @labelColor,
168
- z: @z+1
169
- )
170
-
171
- @label.y = @y + @sliderLine.width / 2 - @label.height / 2
172
-
173
- @nameLabel = Text.new(
174
- @displayName.to_s,
175
- x: @x, y: @y - @size * 3 - @size,
176
- size: @size * 2.5,
177
- color: @labelColor,
178
- z: @z+2
179
- )
180
-
181
- setValue(@value)
182
-
183
- if @showValue == false
184
- @label.remove
185
- end
186
- end
187
- end
188
- end
1
+ module Savio
2
+ class Slider
3
+ include IORenderable
4
+
5
+ attr_reader :length, :min, :max, :size, :value
6
+
7
+ @@sliders = []
8
+ def self.sliders
9
+ @@sliders
10
+ end
11
+
12
+ def initialize(args = {})
13
+ @@sliders.push(self)
14
+ super(args)
15
+
16
+ @length = args[:length] || 100
17
+ @min = args[:min] || 0
18
+ @max = args[:max] || 100
19
+
20
+ @style = args[:style] || 'knob'
21
+ if @style != 'knob' && @style != 'fill'
22
+ @style = 'knob'
23
+ end
24
+
25
+ @value = args[:value] || rand(@min..@max)
26
+ @showValue = args[:showValue] || true
27
+
28
+ @labelColor = args[:labelColor] || Savio::Colors::White
29
+ @sliderColor = args[:sliderColor] || Savio::Colors::Gray
30
+ @knobColor = args[:knobColor] || Savio::Colors::Green
31
+
32
+ @onChange = Proc.new {}
33
+
34
+ build()
35
+ end
36
+
37
+ def onChange(&proc)
38
+ @onChange = proc
39
+ end
40
+
41
+ def style=(style)
42
+ if style == 'knob' || style == 'fill'
43
+ @style = style
44
+ rebuild()
45
+ end
46
+ end
47
+ def length=(length)
48
+ @length = length.clamp(1, Window.width-@x)
49
+ rebuild()
50
+ end
51
+ def showValue=(state)
52
+ @showValue = state
53
+ rebuild()
54
+ end
55
+ def labelColor=(c)
56
+ @labelColor = c
57
+ rebuild()
58
+ end
59
+ def sliderColor=(c)
60
+ @sliderColor = c
61
+ rebuild()
62
+ end
63
+ def knobColor=(c)
64
+ @knobColor = c
65
+ rebuild()
66
+ end
67
+ def min=(x)
68
+ @min = x
69
+ setValue(@value)
70
+ end
71
+ def max=(x)
72
+ @max = x
73
+ setValue(@value)
74
+ end
75
+
76
+ def renderKnobTo(x)
77
+ if x.between?(@x, @x+@length)
78
+ case @style
79
+ when 'knob'
80
+ @knob.x = x
81
+ when 'fill'
82
+ @knob.width = x - @x
83
+ end
84
+ end
85
+ end
86
+
87
+ def moveKnob(x)
88
+ if x.between?(@x, @x+@length)
89
+ renderKnobTo(x)
90
+
91
+ to_max = @max
92
+ to_min = @min
93
+ from_max = @x + @length
94
+ from_min = @x
95
+ pos = x
96
+ @value = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min)
97
+ if @showValue == true
98
+ @label.text = @value.round(2).to_s
99
+ end
100
+ @onChange.call
101
+ end
102
+ end
103
+
104
+ def value=(value)
105
+ setValue(value)
106
+ end
107
+
108
+ def setValue(value)
109
+ if value.between?(@min, @max)
110
+ to_max = @x + @length
111
+ to_min = @x
112
+ from_max = @max.to_f
113
+ from_min = @min.to_f
114
+ pos = value
115
+ knobX = (((to_max - to_min) * (pos - from_min)) / (from_max - from_min) + to_min)
116
+ @value = value
117
+ if @showValue == true
118
+ @label.text = @value.round(2).to_s
119
+ end
120
+ renderKnobTo(knobX)
121
+ else
122
+ setValue(@min)
123
+ end
124
+ end
125
+
126
+ def remove()
127
+ super()
128
+ @sliderLine.remove
129
+ @knob.remove
130
+ @label.remove
131
+ @nameLabel.remove
132
+ end
133
+
134
+ def add()
135
+ super()
136
+ @sliderLine.add
137
+ @knob.add
138
+ @label.add
139
+ @nameLabel.add
140
+ end
141
+
142
+ def build()
143
+ @shown = true
144
+
145
+ @sliderLine = Line.new(
146
+ x1: @x, y1: @y,
147
+ x2: @x+@length, y2: @y,
148
+ width: @size,
149
+ color: @sliderColor,
150
+ z: @z
151
+ )
152
+
153
+ case @style
154
+ when 'knob'
155
+ @knob = Circle.new(
156
+ x: @x, y: @y,
157
+ radius: @size * 1.2,
158
+ color: @knobColor,
159
+ z: @z+1
160
+ )
161
+ when 'fill'
162
+ @knob = Rectangle.new(
163
+ x: @x, y: @y,
164
+ width: 0 , height: @size,
165
+ color: @knobColor,
166
+ z: @z+1
167
+ )
168
+ end
169
+
170
+ @label = Text.new(
171
+ @value.to_s,
172
+ x: @x + @length + @size, y: @y - @size * 1.75,
173
+ size: @size * 2.5,
174
+ color: @labelColor,
175
+ z: @z+1
176
+ )
177
+
178
+ @label.y = @y + @sliderLine.width / 2 - @label.height / 2
179
+
180
+ @nameLabel = Text.new(
181
+ @displayName.to_s,
182
+ x: @x, y: @y - @size * 3 - @size,
183
+ size: @size * 2.5,
184
+ color: @labelColor,
185
+ z: @z+2
186
+ )
187
+
188
+ setValue(@value)
189
+
190
+ if @showValue == false
191
+ @label.remove
192
+ end
193
+ end
194
+ end
195
+ end
data/lib/savio/hsv2rgb.rb CHANGED
@@ -1,36 +1,78 @@
1
- module Savio
2
- def self.hsv2rgb(hue, saturation, value)
3
-
4
- hue = hue.to_f
5
- saturation = saturation.to_f
6
- value = value.to_f
7
-
8
- chroma = (value * saturation).to_f
9
- hPrime = hue/60.0
10
- x = (chroma * (1 - (hPrime % 2 - 1).abs)).to_f
11
-
12
- if 0 <= hPrime && hPrime < 1
13
- rgb = [chroma, x, 0]
14
- elsif 1 <= hPrime && hPrime < 2
15
- rgb = [x, chroma, 0]
16
- elsif 2 <= hPrime && hPrime < 3
17
- rgb = [0, chroma, x]
18
- elsif 3 <= hPrime && hPrime < 4
19
- rgb = [0, x, chroma]
20
- elsif 4 <= hPrime && hPrime < 5
21
- rgb = [x, 0, chroma]
22
- elsif 5 <= hPrime && hPrime < 6
23
- rgb = [chroma, 0, x]
24
- else
25
- rgb = [0,0,0]
26
- end
27
-
28
- match = (value - chroma).to_f
29
-
30
- rgb[0] = (rgb[0] + match).to_f
31
- rgb[1] = (rgb[1] + match).to_f
32
- rgb[2] = (rgb[2] + match).to_f
33
-
34
- return rgb
35
- end
36
- end
1
+ module Savio
2
+ RgbColor = Struct.new(:r, :g, :b) do
3
+ def self.newFromHSV(hsv)
4
+ rgb = hsv.to_rgb()
5
+ return RgbColor.new(rgb.r,rgb.g,rgb.b)
6
+ end
7
+ def to_hsv()
8
+ max = [r, g, b].max
9
+ min = [r, g, b].min
10
+ delta = max - min
11
+ v = max
12
+
13
+ if (max != 0.0)
14
+ s = delta / max
15
+ else
16
+ s = 0.0
17
+ end
18
+
19
+ if (s == 0.0)
20
+ h = 0.0
21
+ else
22
+ if (r == max)
23
+ h = 0 + (g - b) / delta
24
+ elsif (g == max)
25
+ h = 2 + (b - r) / delta
26
+ elsif (b == max)
27
+ h = 4 + (r - g) / delta
28
+ end
29
+
30
+ h *= 60.0
31
+
32
+ if (h < 0)
33
+ h += 360.0
34
+ end
35
+ end
36
+ return HsvColor.new(h,s,v)
37
+ end
38
+ end
39
+ HsvColor = Struct.new(:h, :s, :v) do
40
+ def self.newFromRGB(rgb)
41
+ hsv = rgb.to_hsv()
42
+ return HsvColor.new(hsv.h,hsv.s,hsv.v)
43
+ end
44
+ def to_rgb()
45
+ hue = h.to_f
46
+ saturation = s.to_f
47
+ value = v.to_f
48
+
49
+ chroma = (value * saturation).to_f
50
+ hPrime = hue/60.0
51
+ x = (chroma * (1 - (hPrime % 2 - 1).abs)).to_f
52
+
53
+ if 0 <= hPrime && hPrime < 1
54
+ rgb = [chroma, x, 0]
55
+ elsif 1 <= hPrime && hPrime < 2
56
+ rgb = [x, chroma, 0]
57
+ elsif 2 <= hPrime && hPrime < 3
58
+ rgb = [0, chroma, x]
59
+ elsif 3 <= hPrime && hPrime < 4
60
+ rgb = [0, x, chroma]
61
+ elsif 4 <= hPrime && hPrime < 5
62
+ rgb = [x, 0, chroma]
63
+ elsif 5 <= hPrime && hPrime < 6
64
+ rgb = [chroma, 0, x]
65
+ else
66
+ rgb = [0,0,0]
67
+ end
68
+
69
+ match = (value - chroma).to_f
70
+
71
+ rgb[0] = (rgb[0] + match).to_f
72
+ rgb[1] = (rgb[1] + match).to_f
73
+ rgb[2] = (rgb[2] + match).to_f
74
+
75
+ return RgbColor.new(rgb[0],rgb[1],rgb[2])
76
+ end
77
+ end
78
+ end