Class: MittensUi::ColorPicker
- Inherits:
-
Object
- Object
- MittensUi::ColorPicker
- 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.
Instance Attribute Summary collapse
-
#selected ⇒ Boolean
readonly
Whether the user selected a color.
Instance Method Summary collapse
-
#hex ⇒ String
Returns the selected color as a hex string.
-
#initialize(options = {}) {|picker| ... } ⇒ ColorPicker
constructor
Creates a new ColorPicker dialog and opens it immediately.
-
#rgb ⇒ Array<Integer>
Returns the selected color as an RGB array.
-
#rgba ⇒ Array<Integer>
Returns the selected color as an RGBA array.
-
#selected? ⇒ Boolean
Returns whether the user selected a color.
Constructor Details
#initialize(options = {}) {|picker| ... } ⇒ ColorPicker
Creates a new ColorPicker dialog and opens it immediately.
40 41 42 43 44 45 46 47 48 |
# File 'lib/mittens_ui/colorpicker.rb', line 40 def initialize( = {}, &block) @title = .fetch(:title, 'Pick a Color') @default = .fetch(:default, nil) @alpha = .fetch(:alpha, false) @selected = false @color = nil open_dialog(&block) end |
Instance Attribute Details
#selected ⇒ Boolean (readonly)
Returns whether the user selected a color.
30 31 32 |
# File 'lib/mittens_ui/colorpicker.rb', line 30 def selected @selected end |
Instance Method Details
#hex ⇒ String
Returns the selected color as a hex string. Returns the default or “#000000” if cancelled.
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 |
#rgb ⇒ Array<Integer>
Returns the selected color as an RGB array. Values are in the range 0-255.
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 |
#rgba ⇒ Array<Integer>
Returns the selected color as an RGBA array. Values are in the range 0-255.
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.
53 54 55 |
# File 'lib/mittens_ui/colorpicker.rb', line 53 def selected? @selected end |