Method: RBPDF#Gradient

Defined in:
lib/rbpdf.rb

#Gradient(type, coords, stops, background = [], antialias = false) ⇒ Object Also known as: gradient

Output gradient.

@param int :type

type of gradient (1 Function-based shading; 2 Axial shading; 3 Radial shading; 4 Free-form Gouraud-shaded triangle mesh; 5 Lattice-form Gouraud-shaded triangle mesh; 6 Coons patch mesh; 7 Tensor-product patch mesh). (Not all types are currently supported)

@param array :coords

array of coordinates.

@param array :stops

array gradient color components: color = array of GRAY, RGB or CMYK color components; offset = (0 to 1) represents a location along the gradient vector; exponent = exponent of the exponential interpolation function (default = 1).

@param array :background

An array of colour components appropriate to the colour space, specifying a single background colour value.

@param boolean :antialias

A flag indicating whether to filter the shading function to prevent aliasing artifacts.

@since 3.1.000 (2008-06-09)
@access public


11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
# File 'lib/rbpdf.rb', line 11914

def Gradient(type, coords, stops, background=[], antialias=false)
  n = @gradients.size + 1
  @gradients[n] = {}
  @gradients[n]['type'] = type
  @gradients[n]['coords'] = coords
  @gradients[n]['antialias'] = antialias
  @gradients[n]['colors'] = []
  @gradients[n]['transparency'] = false
  # color space
  numcolspace = stops[0]['color'].size
  bcolor = background
  case numcolspace
  when 4 # CMYK
    @gradients[n]['colspace'] = 'DeviceCMYK'
    unless background.empty?
      @gradients[n]['background'] = sprintf('%.3f %.3f %.3f %.3f', bcolor[0]/100.0, bcolor[1]/100.0, bcolor[2]/100.0, bcolor[3]/100.0)
    end
  when 3 # RGB
    @gradients[n]['colspace'] = 'DeviceRGB'
    unless background.empty?
      @gradients[n]['background'] = sprintf('%.3f %.3f %.3f', bcolor[0]/255.0, bcolor[1]/255.0, bcolor[2]/255.0)
    end
  when 1 # Gray scale
    @gradients[n]['colspace'] = 'DeviceGray'
    unless background.empty?
      @gradients[n]['background'] = sprintf('%.3f', bcolor[0]/255.0)
    end
  end
  num_stops = stops.size
  last_stop_id = num_stops - 1
  stops.each_with_index do |stop, key|
    @gradients[n]['colors'][key] = {}
    # offset represents a location along the gradient vector
    if stop['offset']
      @gradients[n]['colors'][key]['offset'] = stop['offset']
    else
      if key == 0
        @gradients[n]['colors'][key]['offset'] = 0
      elsif key == last_stop_id
        @gradients[n]['colors'][key]['offset'] = 1
      else
        offsetstep = (1 - @gradients[n]['colors'][key - 1]['offset']) / (num_stops - key)
        @gradients[n]['colors'][key]['offset'] = @gradients[n]['colors'][key - 1]['offset'] + offsetstep
      end
    end
    if stop['opacity']
      @gradients[n]['colors'][key]['opacity'] = stop['opacity']
      if stop['opacity'] < 1
        @gradients[n]['transparency'] = true
      end
    else
      @gradients[n]['colors'][key]['opacity'] = 1
    end
    # exponent for the exponential interpolation function
    if stop['exponent']
      @gradients[n]['colors'][key]['exponent'] = stop['exponent']
    else
      @gradients[n]['colors'][key]['exponent'] = 1
    end
    # set colors
    color = stop['color']
    case numcolspace
    when 4 # CMYK
      @gradients[n]['colors'][key]['color'] = sprintf('%.3f %.3f %.3f %.3f', color[0]/100.0, color[1]/100.0, color[2]/100.0, color[3]/100.0)
    when 3 # RGB
      @gradients[n]['colors'][key]['color'] = sprintf('%.3f %.3f %.3f', color[0]/255.0, color[1]/255.0, color[2]/255.0)
    when 1 # Gray scale
      @gradients[n]['colors'][key]['color'] = sprintf('%.3f', color[0]/255.0)
    end
  end
  if @gradients[n]['transparency']
    # paint luminosity gradient
    out("/TGS#{n} gs")
  end
  # paint the gradient
  out("/Sh#{n} sh")
  # restore previous Graphic State
  out('Q')
end