Module: Plushie::Type::Gradient

Defined in:
lib/plushie/type/gradient.rb

Overview

Gradient specification for widget backgrounds.

Currently supports linear gradients with angle and color stops.

Examples:

Gradient.linear(45, [[0.0, :red], [1.0, :blue]])

Class Method Summary collapse

Class Method Details

.encode(value) ⇒ Hash

Encode a gradient for the wire protocol.

Parameters:

  • value (Hash)

    gradient specification

Returns:

  • (Hash)


33
34
35
36
37
38
# File 'lib/plushie/type/gradient.rb', line 33

def encode(value)
  case value
  when Hash then value
  else raise ArgumentError, "invalid gradient: #{value.inspect}"
  end
end

.linear(angle, stops) ⇒ Hash

Create a linear gradient.

Parameters:

  • angle (Numeric)

    angle in degrees

  • stops (Array<Array(Float, String|Symbol)>)

    color stops as [offset, color] pairs

Returns:

  • (Hash)

    wire-ready gradient map



19
20
21
22
23
24
25
26
27
# File 'lib/plushie/type/gradient.rb', line 19

def linear(angle, stops)
  {
    type: "linear",
    angle: angle,
    stops: stops.map { |offset, color|
      {offset: offset.to_f, color: Color.cast(color)}
    }
  }
end