Class: Skia::Shader

Inherits:
Base
  • Object
show all
Defined in:
lib/skia/shader.rb

Instance Attribute Summary

Attributes inherited from Base

#ptr

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#null?, release_callback, #to_ptr

Constructor Details

#initialize(ptr) ⇒ Shader

Returns a new instance of Shader.



5
6
7
# File 'lib/skia/shader.rb', line 5

def initialize(ptr)
  super(ptr, :sk_shader_unref)
end

Class Method Details

.linear_gradient(start_point, end_point, colors, positions = nil, tile_mode: :clamp, matrix: nil) ⇒ Object

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/skia/shader.rb', line 15

def self.linear_gradient(start_point, end_point, colors, positions = nil, tile_mode: :clamp, matrix: nil)
  points = FFI::MemoryPointer.new(Native::SKPoint, 2)
  start_struct = start_point.to_struct
  end_struct = end_point.to_struct
  points[0].write_bytes(start_struct.to_ptr.read_bytes(Native::SKPoint.size))
  points[1].write_bytes(end_struct.to_ptr.read_bytes(Native::SKPoint.size))

  color_values = colors.map { |c| c.is_a?(Color) ? c.to_i : c }
  colors_ptr = FFI::MemoryPointer.new(:uint32, color_values.size)
  colors_ptr.write_array_of_uint32(color_values)

  positions_ptr = nil
  if positions
    positions_ptr = FFI::MemoryPointer.new(:float, positions.size)
    positions_ptr.write_array_of_float(positions.map(&:to_f))
  end

  matrix_ptr = matrix&.to_struct

  ptr = Native.sk_shader_new_linear_gradient(
    points,
    colors_ptr,
    positions_ptr,
    color_values.size,
    tile_mode,
    matrix_ptr
  )
  raise Error, 'Failed to create linear gradient shader' if ptr.nil? || ptr.null?

  new(ptr)
end

.radial_gradient(center, radius, colors, positions = nil, tile_mode: :clamp, matrix: nil) ⇒ Object

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/skia/shader.rb', line 47

def self.radial_gradient(center, radius, colors, positions = nil, tile_mode: :clamp, matrix: nil)
  center_struct = center.to_struct

  color_values = colors.map { |c| c.is_a?(Color) ? c.to_i : c }
  colors_ptr = FFI::MemoryPointer.new(:uint32, color_values.size)
  colors_ptr.write_array_of_uint32(color_values)

  positions_ptr = nil
  if positions
    positions_ptr = FFI::MemoryPointer.new(:float, positions.size)
    positions_ptr.write_array_of_float(positions.map(&:to_f))
  end

  matrix_ptr = matrix&.to_struct

  ptr = Native.sk_shader_new_radial_gradient(
    center_struct,
    radius.to_f,
    colors_ptr,
    positions_ptr,
    color_values.size,
    tile_mode,
    matrix_ptr
  )
  raise Error, 'Failed to create radial gradient shader' if ptr.nil? || ptr.null?

  new(ptr)
end

.sweep_gradient(center, colors, positions = nil, tile_mode: :clamp, start_angle: 0.0, end_angle: 360.0, matrix: nil) ⇒ Object

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/skia/shader.rb', line 76

def self.sweep_gradient(center, colors, positions = nil, tile_mode: :clamp, start_angle: 0.0, end_angle: 360.0,
                        matrix: nil)
  center_struct = center.to_struct

  color_values = colors.map { |c| c.is_a?(Color) ? c.to_i : c }
  colors_ptr = FFI::MemoryPointer.new(:uint32, color_values.size)
  colors_ptr.write_array_of_uint32(color_values)

  positions_ptr = nil
  if positions
    positions_ptr = FFI::MemoryPointer.new(:float, positions.size)
    positions_ptr.write_array_of_float(positions.map(&:to_f))
  end

  matrix_ptr = matrix&.to_struct

  ptr = Native.sk_shader_new_sweep_gradient(
    center_struct,
    colors_ptr,
    positions_ptr,
    color_values.size,
    tile_mode,
    start_angle.to_f,
    end_angle.to_f,
    matrix_ptr
  )
  raise Error, 'Failed to create sweep gradient shader' if ptr.nil? || ptr.null?

  new(ptr)
end

.two_point_conical_gradient(start_point, start_radius, end_point, end_radius, colors, positions = nil, tile_mode: :clamp, matrix: nil) ⇒ Object

Raises:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/skia/shader.rb', line 107

def self.two_point_conical_gradient(start_point, start_radius, end_point, end_radius, colors, positions = nil,
                                    tile_mode: :clamp, matrix: nil)
  unless Native.function_available?(:sk_shader_new_two_point_conical_gradient)
    raise UnsupportedOperationError,
          'Shader.two_point_conical_gradient is not supported by the current libSkiaSharp build'
  end

  start_struct = start_point.to_struct
  end_struct = end_point.to_struct

  color_values = colors.map { |c| c.is_a?(Color) ? c.to_i : c }
  colors_ptr = FFI::MemoryPointer.new(:uint32, color_values.size)
  colors_ptr.write_array_of_uint32(color_values)

  positions_ptr = nil
  if positions
    positions_ptr = FFI::MemoryPointer.new(:float, positions.size)
    positions_ptr.write_array_of_float(positions.map(&:to_f))
  end

  matrix_ptr = matrix&.to_struct

  ptr = Native.sk_shader_new_two_point_conical_gradient(
    start_struct,
    start_radius.to_f,
    end_struct,
    end_radius.to_f,
    colors_ptr,
    positions_ptr,
    color_values.size,
    tile_mode,
    matrix_ptr
  )
  raise Error, 'Failed to create two-point conical gradient shader' if ptr.nil? || ptr.null?

  new(ptr)
end

.wrap(ptr) ⇒ Object



9
10
11
12
13
# File 'lib/skia/shader.rb', line 9

def self.wrap(ptr)
  return nil if ptr.nil? || ptr.null?

  new(ptr)
end