Sha256: ba3bc713a910a1e547f9d8e8c07a8016697a72a98f5e5b71d958c3a8c3fabf11

Contents?: true

Size: 1.65 KB

Versions: 5

Compression:

Stored size: 1.65 KB

Contents

# encoding: utf-8

module Fidgit
  class ColorPicker < Composite
    CHANNELS = [:red, :green, :blue]
    DEFAULT_CHANNEL_NAMES = CHANNELS.map {|c| c.to_s.capitalize }

    INDICATOR_HEIGHT = 25

    event :changed

    def color; @color.dup; end

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

      publish :changed, @color.dup

      value
    end

    # @param (see Composite#initialize)
    # @option (see Composite#initialize)
    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
      pack :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

    protected
    # Use block as an event handler.
    def post_init_block(&block)
      subscribe :changed, &block
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fidgit-0.0.6alpha lib/fidgit/elements/color_picker.rb
fidgit-0.0.5alpha lib/fidgit/elements/color_picker.rb
fidgit-0.0.4alpha lib/fidgit/elements/color_picker.rb
fidgit-0.0.3alpha lib/fidgit/elements/color_picker.rb
fidgit-0.0.2alpha lib/fidgit/elements/color_picker.rb