Class: Rainbow::Gradient

Inherits:
Object
  • Object
show all
Defined in:
lib/rainbow/gradient.rb

Constant Summary collapse

SUPPORTED_ANGLES =
[0, 180]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, args) ⇒ Gradient

wdith - The gradient width height - The gradient height args - Hash that describes the property of the gradient

{
  blend_mode: 'norm|diss|dark|...',
  dither: true|false,
  opacity: 0..100,
  gradient: {
    type: 'solid|noise',
    smoothness: 0..100,
    color_ranges: [],
    opacity_ranges: []
  },
  reverse: true|false,
  style: 'linear|radial|angle|reflected|diamond',
  align_with_layer: true|false,
  angle: 0..360,
  scale: 10..150
}


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rainbow/gradient.rb', line 30

def initialize(width, height, args)
  @width  = width
  @height = height
  @args   = args

  @style          = args[:style]
  @color_ranges   = args[:gradient][:color_ranges]
  @opacity_ranges = OpacityRanges.new(args[:gradient][:opacity_ranges], self)

  assert_arguments!
  compute_scale_variables!
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



4
5
6
# File 'lib/rainbow/gradient.rb', line 4

def args
  @args
end

#canvasObject (readonly)

Returns the value of attribute canvas.



5
6
7
# File 'lib/rainbow/gradient.rb', line 5

def canvas
  @canvas
end

#color_rangesObject (readonly)

Returns the value of attribute color_ranges.



4
5
6
# File 'lib/rainbow/gradient.rb', line 4

def color_ranges
  @color_ranges
end

#heightObject (readonly)

Returns the value of attribute height.



4
5
6
# File 'lib/rainbow/gradient.rb', line 4

def height
  @height
end

#opacity_rangesObject (readonly)

Returns the value of attribute opacity_ranges.



4
5
6
# File 'lib/rainbow/gradient.rb', line 4

def opacity_ranges
  @opacity_ranges
end

#scale_end_location_in_pixelObject (readonly)

Returns the value of attribute scale_end_location_in_pixel.



6
7
8
# File 'lib/rainbow/gradient.rb', line 6

def scale_end_location_in_pixel
  @scale_end_location_in_pixel
end

#scale_half_distance_in_pixelObject (readonly)

Returns the value of attribute scale_half_distance_in_pixel.



6
7
8
# File 'lib/rainbow/gradient.rb', line 6

def scale_half_distance_in_pixel
  @scale_half_distance_in_pixel
end

#scale_mid_location_in_pixelObject (readonly)

Returns the value of attribute scale_mid_location_in_pixel.



6
7
8
# File 'lib/rainbow/gradient.rb', line 6

def scale_mid_location_in_pixel
  @scale_mid_location_in_pixel
end

#scale_start_location_in_pixelObject (readonly)

Returns the value of attribute scale_start_location_in_pixel.



6
7
8
# File 'lib/rainbow/gradient.rb', line 6

def scale_start_location_in_pixel
  @scale_start_location_in_pixel
end

#styleObject (readonly)

Returns the value of attribute style.



4
5
6
# File 'lib/rainbow/gradient.rb', line 4

def style
  @style
end

#widthObject (readonly)

Returns the value of attribute width.



4
5
6
# File 'lib/rainbow/gradient.rb', line 4

def width
  @width
end

Instance Method Details

#align_with_layerObject



77
78
79
# File 'lib/rainbow/gradient.rb', line 77

def align_with_layer
  args.fetch(:align_with_layer, true)
end

#angleObject



81
82
83
84
# File 'lib/rainbow/gradient.rb', line 81

def angle
  value = args.fetch(:angle, 0)
  value % 360
end

#blend_modeObject



53
54
55
# File 'lib/rainbow/gradient.rb', line 53

def blend_mode
  args.fetch(:blend_mode, 'norm')
end

#create_canvasObject



43
44
45
46
47
48
49
50
51
# File 'lib/rainbow/gradient.rb', line 43

def create_canvas
  @canvas = ChunkyPNG::Canvas.new(width, height, ChunkyPNG::Color::TRANSPARENT)

  paint_canvas!(@canvas)
  set_opacity!(@canvas) if opacity != 100
  reverse!(@canvas) if reverse || angle == 180

  @canvas
end

#ditherObject



57
58
59
# File 'lib/rainbow/gradient.rb', line 57

def dither
  args.fetch(:dither, false)
end

#opacityObject



61
62
63
# File 'lib/rainbow/gradient.rb', line 61

def opacity
  args.fetch(:opacity, 100)
end

#reverseObject



73
74
75
# File 'lib/rainbow/gradient.rb', line 73

def reverse
  args.fetch(:reverse, false)
end

#save_as_png(path) ⇒ Object



94
95
96
97
98
99
# File 'lib/rainbow/gradient.rb', line 94

def save_as_png(path)
  create_canvas unless canvas
  canvas.save(path, :fast_rgba)

  path
end

#scaleObject



86
87
88
# File 'lib/rainbow/gradient.rb', line 86

def scale
  args.fetch(:scale, 100)
end

#scale_100?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/rainbow/gradient.rb', line 90

def scale_100?
  scale == 100
end

#smoothnessObject



69
70
71
# File 'lib/rainbow/gradient.rb', line 69

def smoothness
  args[:gradient][:smoothness]
end

#typeObject



65
66
67
# File 'lib/rainbow/gradient.rb', line 65

def type
  args[:gradient][:type]
end