Class: Fidgit::ColorPicker

Inherits:
Composite show all
Defined in:
lib/fidgit/elements/color_picker.rb

Constant Summary collapse

CHANNELS =
[:red, :green, :blue]
DEFAULT_CHANNEL_NAMES =
CHANNELS.map {|c| c.to_s.capitalize }
INDICATOR_HEIGHT =
25

Constants inherited from Composite

Fidgit::Composite::DEBUG_BORDER_COLOR

Constants inherited from Element

Element::DEFAULT_SCHEMA_FILE, Element::VALID_ALIGN_H, Element::VALID_ALIGN_V

Instance Attribute Summary

Attributes inherited from Packer

#spacing_h, #spacing_v

Attributes inherited from Element

#align_h, #align_v, #background_color, #border_thickness, #font, #padding_bottom, #padding_left, #padding_right, #padding_top, #parent, #tip, #z

Instance Method Summary collapse

Methods inherited from Container

#add, #button, #clear, #color_picker, #color_well, #combo_box, #file_browser, #grid, #group, #hit_element, #horizontal, #image_frame, #insert, #label, #list, #radio_button, #remove, #scroll_area, #scroll_window, #slider, #text_area, #to_s, #toggle_button, #update, #vertical, #write_tree, #x=, #y=

Methods inherited from Element

#default, #drag?, #draw, #draw_frame, #draw_rect, #enabled=, #enabled?, #height, #height=, #hit?, #max_height, #max_width, #min_height, #min_width, new, original_new, #outer_height, #outer_width, #recalc, schema, #to_s, #update, #width, #width=, #with, #x, #x=, #y, #y=

Methods included from Event

#events, included, new_event_handlers, #publish, #subscribe, #unsubscribe

Constructor Details

#initialize(options = {}, &block) ⇒ ColorPicker

Returns a new instance of ColorPicker.


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fidgit/elements/color_picker.rb', line 27

def initialize(options = {}, &block)
  options = {
    padding: 0,
    spacing: 0,
    channel_names: DEFAULT_CHANNEL_NAMES,
    color: default(:color),
    indicator_height: default(:indicator_height),
  }.merge! options

  @color = options[:color].dup
  @indicator_height = options[:indicator_height]

  super(options)

  slider_width = width
  vertical do
    @sliders = {}
    CHANNELS.each_with_index do |channel, i|
      @sliders[channel] = slider(value: @color.send(channel), range: 0..255, width: slider_width,
                                 tip: options[:channel_names][i]) do |sender, value|
        @color.send "#{channel}=", value
        @indicator.background_color = @color
        publish :changed, @color.dup
      end
    end

    @indicator = label '', background_color: @color, width: slider_width, height: @indicator_height
  end
end

Instance Method Details

#colorObject


12
# File 'lib/fidgit/elements/color_picker.rb', line 12

def color; @color.dup; end

#color=(value) ⇒ Object


14
15
16
17
18
19
20
21
22
23
# File 'lib/fidgit/elements/color_picker.rb', line 14

def color=(value)
  @color = value.dup
  CHANNELS.each do |channel|
    @sliders[channel].value = @color.send channel
  end

  publish :changed, @color.dup

  value
end