Class: Savio::ColorSlider
- Inherits:
-
Object
- Object
- Savio::ColorSlider
- Includes:
- IORenderable
- Defined in:
- lib/savio/ColorSlider.rb
Constant Summary collapse
- @@colorSliders =
[]
Instance Attribute Summary collapse
-
#animating ⇒ Object
readonly
Returns the value of attribute animating.
-
#baseColor ⇒ Object
Returns the value of attribute baseColor.
-
#knobColor ⇒ Object
Returns the value of attribute knobColor.
-
#optionsShown ⇒ Object
readonly
Returns the value of attribute optionsShown.
-
#sectors ⇒ Object
readonly
Returns the value of attribute sectors.
-
#textColor ⇒ Object
Returns the value of attribute textColor.
Attributes included from IORenderable
#allowDrag, #displayName, #draggingEnabled, #duplicate, #enabled, #shown, #size, #x, #y, #z
Class Method Summary collapse
Instance Method Summary collapse
- #add ⇒ Object
- #animate ⇒ Object
- #build ⇒ Object
- #calculateStep(desire, start, steps) ⇒ Object
- #chord(placement) ⇒ Object
- #hex ⇒ Object
- #hideOptions ⇒ Object
- #hsv ⇒ Object
-
#initialize(args = {}) ⇒ ColorSlider
constructor
A new instance of ColorSlider.
- #remove ⇒ Object
- #rgb ⇒ Object
- #setValue(angle) ⇒ Object
- #showOptions ⇒ Object
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
#animating ⇒ Object (readonly)
Returns the value of attribute animating.
5 6 7 |
# File 'lib/savio/ColorSlider.rb', line 5 def animating @animating end |
#baseColor ⇒ Object
Returns the value of attribute baseColor.
5 6 7 |
# File 'lib/savio/ColorSlider.rb', line 5 def baseColor @baseColor end |
#knobColor ⇒ Object
Returns the value of attribute knobColor.
5 6 7 |
# File 'lib/savio/ColorSlider.rb', line 5 def knobColor @knobColor end |
#optionsShown ⇒ Object (readonly)
Returns the value of attribute optionsShown.
5 6 7 |
# File 'lib/savio/ColorSlider.rb', line 5 def @optionsShown end |
#sectors ⇒ Object (readonly)
Returns the value of attribute sectors.
5 6 7 |
# File 'lib/savio/ColorSlider.rb', line 5 def sectors @sectors end |
#textColor ⇒ Object
Returns the value of attribute textColor.
5 6 7 |
# File 'lib/savio/ColorSlider.rb', line 5 def textColor @textColor end |
Class Method Details
.colorSliders ⇒ Object
8 9 10 |
# File 'lib/savio/ColorSlider.rb', line 8 def self.colorSliders @@colorSliders end |
Instance Method Details
#add ⇒ Object
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 |
#animate ⇒ Object
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 |
#build ⇒ Object
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 |
#hex ⇒ Object
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 |
#hideOptions ⇒ Object
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 |
#hsv ⇒ Object
137 138 139 |
# File 'lib/savio/ColorSlider.rb', line 137 def hsv() return HsvColor.new(@color[0],@color[1],@color[2]) end |
#remove ⇒ Object
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 |
#rgb ⇒ Object
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 |
#showOptions ⇒ Object
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 |