Class: Savio::ColorSlider

Inherits:
Object
  • Object
show all
Includes:
IORenderable
Defined in:
lib/savio/ColorSlider.rb

Constant Summary collapse

@@colorSliders =
[]

Instance Attribute Summary collapse

Attributes included from IORenderable

#allowDrag, #displayName, #draggingEnabled, #duplicate, #enabled, #shown, #size, #x, #y, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IORenderable

#context, #drag, #dragType=, #endDrag, #kill, #rebuild

Constructor Details

#initialize(args = {}) ⇒ ColorSlider

Returns a new instance of ColorSlider.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/savio/ColorSlider.rb', line 12

def initialize(args = {})
  args[:size] = args[:size] || 80
  super(args)
  @@colorSliders.push(self)

  @color = args[:color] || HsvColor.new(rand(0..360), rand(0.0..1.0),rand(0.0..1.0))
  @hue = @color[0]

  @baseColor = args[:baseColor] || Savio::Colors::White
  @knobColor = args[:baseColor] || Savio::Colors::Gray
  @textColor = args[:textColor] || Savio::Colors::Black
  @sectors = args[:sectors] || 64

  @optionsShown = false
  @steps = 0
  @animating = false

  @radiusStep = 0
  @yStep = 0

  build()
end

Instance Attribute Details

#animatingObject (readonly)

Returns the value of attribute animating.



5
6
7
# File 'lib/savio/ColorSlider.rb', line 5

def animating
  @animating
end

#baseColorObject

Returns the value of attribute baseColor.



5
6
7
# File 'lib/savio/ColorSlider.rb', line 5

def baseColor
  @baseColor
end

#knobColorObject

Returns the value of attribute knobColor.



5
6
7
# File 'lib/savio/ColorSlider.rb', line 5

def knobColor
  @knobColor
end

#optionsShownObject (readonly)

Returns the value of attribute optionsShown.



5
6
7
# File 'lib/savio/ColorSlider.rb', line 5

def optionsShown
  @optionsShown
end

#sectorsObject (readonly)

Returns the value of attribute sectors.



5
6
7
# File 'lib/savio/ColorSlider.rb', line 5

def sectors
  @sectors
end

#textColorObject

Returns the value of attribute textColor.



5
6
7
# File 'lib/savio/ColorSlider.rb', line 5

def textColor
  @textColor
end

Class Method Details

.colorSlidersObject



8
9
10
# File 'lib/savio/ColorSlider.rb', line 8

def self.colorSliders
  @@colorSliders
end

Instance Method Details

#addObject



56
57
58
59
60
61
62
63
# File 'lib/savio/ColorSlider.rb', line 56

def add()
  super()
  @innerCircle.add
  @outerCircle.add
  @knob.add
  @saturationSlider.add
  @valueSlider.add
end

#animateObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/savio/ColorSlider.rb', line 65

def animate()
  animatorThread = Thread.new do
    12.times do
      sleep(1 / 60)
      @steps += 1
      @innerCircle.radius += @radiusStep
      @innerCircle.y += @yStep
      if @steps >= 12
        @steps = 0
        @radiusStep = 0
        @yStep = 0
        @animating = false
      end
    end
  end
end

#buildObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/savio/ColorSlider.rb', line 156

def build()
  @shown = true

  @innerCircle = Circle.new(
    x: @x, y: @y,
    radius: @size * 0.95,
    color: 'black',
    z: @z + 4,
    sectors: @sectors
  )

  @outerCircle = Circle.new(
    x: @x, y: @y,
    radius: @size,
    color: @baseColor,
    z: @z,
    sectors: @sectors
  )

  @knob = Circle.new(
    x: @x + @size, y: @y,
    radius: @size * 0.15,
    color: @knobColor,
    z: @z + 5,
    sectors: @sectors
  )

  placement = chord(1.0)
  @saturationSlider = Slider.new(
    displayName: "Saturation",
    labelColor: @textColor,
    x:@x - @size + placement.adjust + placement.margin,
    y:@y - @size + placement.height,
    z:@z + 1,
    min:0.0,
    max:1.0,
    length:(placement.chord - placement.margin * 2),
    size: @size * 0.06,
    showValue: false
  )

  placement = chord(1.3)
  @valueSlider = Slider.new(
    displayName: "Value",
    labelColor: @textColor,
    x:@x - @size + placement.adjust + placement.margin,
    y:@y - @size + placement.height,
    z: @z + 1,
    min:0.0,
    max:1.0,
    length:(placement.chord - placement.margin * 2),
    size: @size * 0.06,
    showValue: false
  )
  @valueSlider.showValue=false
  @saturationSlider.showValue=false

  @valueSlider.onChange do
    setValue(@hue)
  end

  @saturationSlider.onChange do
    setValue(@hue)
  end

  setValue(@hue)
end

#calculateStep(desire, start, steps) ⇒ Object



82
83
84
85
86
# File 'lib/savio/ColorSlider.rb', line 82

def calculateStep(desire, start, steps)
  delta = desire.to_f - start.to_f
  step = delta/steps
  return step
end

#chord(placement) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/savio/ColorSlider.rb', line 146

def chord(placement)
  margin = 20
  height = @size * placement
  angle = 2 * Math.acos( 1.0 - height / @size.to_f)
  angle = angle * 180/Math::PI
  chord = 2 * Math.sqrt(2 * @size * height - height**2)
  adjust = (@size * 2 - chord) / 2
  return Struct.new(:margin, :height, :angle, :chord, :adjust).new(margin,height,angle,chord,adjust)
end

#hexObject



141
142
143
144
# File 'lib/savio/ColorSlider.rb', line 141

def hex()
  rgb = RgbColor.newFromHSV(@color)
  return ("#%02x%02x%02x" % [rgb[0]*255,rgb[1]*255,rgb[2]*255])
end

#hideOptionsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/savio/ColorSlider.rb', line 104

def hideOptions()
  if @optionsShown == true
    @optionsShown = false

    @steps = 0
    @radiusStep = 0
    @yStep = 0
    @animating = false

    @radiusStep = calculateStep(@size * 0.95, @innerCircle.radius, 12)
    @yStep = calculateStep(@y, @innerCircle.y, 12)
    @animating = true
    animate()
  end
end

#hsvObject



137
138
139
# File 'lib/savio/ColorSlider.rb', line 137

def hsv()
  return HsvColor.new(@color[0],@color[1],@color[2])
end

#removeObject



47
48
49
50
51
52
53
54
# File 'lib/savio/ColorSlider.rb', line 47

def remove()
  super()
  @innerCircle.remove
  @outerCircle.remove
  @knob.remove
  @saturationSlider.remove
  @valueSlider.remove
end

#rgbObject



133
134
135
# File 'lib/savio/ColorSlider.rb', line 133

def rgb()
  return RgbColor.newFromHSV(@color)
end

#setValue(angle) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/savio/ColorSlider.rb', line 120

def setValue(angle)
  @knob.x = (@size).to_f * Math.cos((angle.to_f % 360.0) * Math::PI/180.0) + @x.to_f
  @knob.y = (@size).to_f * Math.sin((angle.to_f % 360.0) * Math::PI/180.0) + @y.to_f

  @color = HsvColor.new(angle % 360, @saturationSlider.value, @valueSlider.value)
  @hue = @color[0]

  rgb = RgbColor.newFromHSV(@color)
  @innerCircle.color.r = rgb[0]
  @innerCircle.color.g = rgb[1]
  @innerCircle.color.b = rgb[2]
end

#showOptionsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/savio/ColorSlider.rb', line 88

def showOptions()
  if @optionsShown == false
    @optionsShown = true

    @steps = 0
    @radiusStep = 0
    @yStep = 0
    @animating = false

    @radiusStep = calculateStep(0.25 * @size, @innerCircle.radius, 12)
    @yStep = calculateStep(@y - @size + 0.35 * @size, @innerCircle.y, 12)
    @animating = true
    animate()
  end
end