File: blur.rb

package info (click to toggle)
ruby-cairo 1.17.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,532 kB
  • sloc: ruby: 11,997; ansic: 10,183; sh: 48; makefile: 4
file content (45 lines) | stat: -rw-r--r-- 1,087 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
module Cairo
  class Context
    module Blur
      def pseudo_blur(radius=3, &block)
        raise ArgumentError, "should specify block" if block.nil?
        pattern = push_group(Cairo::CONTENT_COLOR_ALPHA, false, &block)

        save do
          set_source(pattern)
          paint(0.5)
        end

        1.step(radius, 1) do |i|
          opacity = 0.075 - 0.005 * i
          next if opacity <= 0
          5.downto(1) do |ratio|
            r = i / ratio.to_f
            r_13 = r / 3.0
            r_23 = 2 * r_13
            [
             [-r, 0],
             [-r_23, r_23],
             [-r_23, r_13],
             [0, r],
             [r_13, r_23],
             [r_23, r_23],
             [r, 0],
             [r_23, -r_13],
             [r_23, -r_23],
             [0, -r],
             [-r_13, -r_23],
             [-r_23, -r_23],
            ].each do |x, y|
              save do
                translate(x, y)
                set_source(pattern)
                paint(opacity)
              end
            end
          end
        end
      end
    end
  end
end