Class: MittensUi::ColorPicker

Inherits:
Object
  • Object
show all
Defined in:
lib/mittens_ui/colorpicker.rb

Overview

A color picker dialog that allows the user to select a color. Wraps Gtk::ColorDialog. Opens immediately on instantiation using an async API. The selected color is accessible via #hex, #rgb, and #rgba.

Examples:

Basic usage

picker = MittensUi::ColorPicker.new
puts picker.hex   # => "#ff0000"
puts picker.rgb   # => [255, 0, 0]
puts picker.rgba  # => [255, 0, 0, 255]

With a default color

picker = MittensUi::ColorPicker.new(default: "#336699")

With alpha channel

picker = MittensUi::ColorPicker.new(alpha: true)

With block — only fires if user selected a color

MittensUi::ColorPicker.new do |color|
  puts color.hex
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|picker| ... } ⇒ ColorPicker

Creates a new ColorPicker dialog and opens it immediately.

Parameters:

  • options (Hash) (defaults to: {})

    configuration options

Options Hash (options):

  • :title (String) — default: "Pick a Color"

    the dialog title

  • :default (String) — default: nil

    a hex color string to pre-select

  • :alpha (Boolean) — default: false

    show alpha channel slider

Yields:

  • (picker)

    called only if the user selected a color

Yield Parameters:



40
41
42
43
44
45
46
47
48
# File 'lib/mittens_ui/colorpicker.rb', line 40

def initialize(options = {}, &block)
  @title    = options.fetch(:title, 'Pick a Color')
  @default  = options.fetch(:default, nil)
  @alpha    = options.fetch(:alpha, false)
  @selected = false
  @color    = nil

  open_dialog(&block)
end

Instance Attribute Details

#selectedBoolean (readonly)

Returns whether the user selected a color.

Returns:

  • (Boolean)

    whether the user selected a color



30
31
32
# File 'lib/mittens_ui/colorpicker.rb', line 30

def selected
  @selected
end

Instance Method Details

#hexString

Returns the selected color as a hex string. Returns the default or “#000000” if cancelled.

Returns:

  • (String)

    hex color string e.g. “#ff0000”



61
62
63
64
65
66
# File 'lib/mittens_ui/colorpicker.rb', line 61

def hex
  return @default || '#000000' unless @selected && @color

  r, g, b = rgb
  "#%02x%02x%02x" % [r, g, b]
end

#rgbArray<Integer>

Returns the selected color as an RGB array. Values are in the range 0-255.

Returns:

  • (Array<Integer>)
    red, green, blue


72
73
74
75
76
77
78
79
80
# File 'lib/mittens_ui/colorpicker.rb', line 72

def rgb
  return [0, 0, 0] unless @selected && @color

  [
    (@color.red   * 255).round,
    (@color.green * 255).round,
    (@color.blue  * 255).round
  ]
end

#rgbaArray<Integer>

Returns the selected color as an RGBA array. Values are in the range 0-255.

Returns:

  • (Array<Integer>)
    red, green, blue, alpha


86
87
88
89
90
91
92
93
94
95
# File 'lib/mittens_ui/colorpicker.rb', line 86

def rgba
  return [0, 0, 0, 255] unless @selected && @color

  [
    (@color.red   * 255).round,
    (@color.green * 255).round,
    (@color.blue  * 255).round,
    (@color.alpha * 255).round
  ]
end

#selected?Boolean

Returns whether the user selected a color.

Returns:

  • (Boolean)


53
54
55
# File 'lib/mittens_ui/colorpicker.rb', line 53

def selected?
  @selected
end