Module: Sass::Script::Functions

Defined in:
lib/compass-recipes/sass_extensions/_error.rb,
lib/compass-recipes/sass_extensions/background_noise.rb,
lib/compass-recipes/sass_extensions/gravatar.rb,
lib/compass-recipes/sass_extensions.rb

Overview

Helper to throw error message

Instance Method Summary collapse

Instance Method Details

#background_noise(kwargs = {}) ⇒ Object



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
# File 'lib/compass-recipes/sass_extensions/background_noise.rb', line 12

def background_noise(kwargs = {})
    opts = {}
    Sass::Util.map_hash({
     "intensity"  => [0..1,          "",   :Number, Sass::Script::Number.new(0.5) ],
     "opacity"    => [0..1,          "",   :Number, Sass::Script::Number.new(0.12)],
     "size"       => [1..512,        "px", :Number, Sass::Script::Number.new(200) ],
     "monochrome" => [[true, false], "",   :Bool,   Sass::Script::Bool.new(true) ]
     }) do |name, (range, units, type, default)|

        if val = kwargs.delete(name)
            assert_type val, type, name
            if range && !range.include?(val.value)
                raise ArgumentError.new("$#{name}: Amount #{val} must be between #{range.first}#{units} and #{range.last}#{units}")
            end
        else
            val = default
        end
        opts[name] = val
    end

    image = ChunkyPNG::Image.new(opts["size"].to_i, opts["size"].to_i)

    for i in (0..(opts["intensity"].to_s.to_f * (opts["size"].to_i**2)))
        x = rand(opts["size"].to_i)
        y = rand(opts["size"].to_i)
        r = rand(255)
        a = rand(255 * opts["opacity"].to_s.to_f)
        color = opts["monochrome"] ? ChunkyPNG::Color.rgba(r, r, r, a) : ChunkyPNG::Color.rgba(r, rand(255), rand(255), a)
        image.set_pixel(x, y, color)
    end

    data = Base64.encode64(image.to_blob).gsub("\n", "")
    Sass::Script::String.new("url('data:image/png;base64,#{data}')")
end

#error(message) ⇒ Object

Raises:

  • (Sass::SyntaxError)


6
7
8
# File 'lib/compass-recipes/sass_extensions/_error.rb', line 6

def error(message)
    raise Sass::SyntaxError, message.value
end

#gravatar_url(kwargs = {}) ⇒ Object



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
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/compass-recipes/sass_extensions/gravatar.rb', line 8

def gravatar_url(kwargs = {})
    opts = {}
    Sass::Util.map_hash({

        'email' => [:String, nil],

        # The URL of a default image to display if the given email address does
        # not have a gravatar.
        'default' => [:String, nil],

        # The default size in pixels for the gravatar image (they're square).
        'size' => [:Number, 48],

        # The maximum allowed MPAA rating for gravatars. This allows you to
        # exclude gravatars that may be out of character for your site.
        'rating' => [:String, 'PG'],

        # Whether or not to display the gravatars using HTTPS instead of HTTP
        'ssl' => [:Bool, Sass::Script::Bool.new(false)],

    }) do |name, (type, default)|

        val = kwargs.delete(name)
        if val
            assert_type val, type, name
        else
            val = default
        end
        opts[name] = val
    end

    email_hash = Digest::MD5::hexdigest(opts['email'].to_s)

    if opts['ssl'].value
        url = "https://secure.gravatar.com/avatar/#{email_hash}.png"
    else
        url = "http://www.gravatar.com/avatar/#{email_hash}.png"
    end

    gravatar_opts = []
    ['rating', 'size', 'default'].each do |opt|
        unless opts[opt].nil?
            value = CGI::escape(opts[opt].to_s)
            gravatar_opts << [opt, value].join('=')
        end
    end

    url << "?#{gravatar_opts.join('&')}" unless gravatar_opts.empty?
    Sass::Script::String.new("url(" + url + ")");
end

#value(number) ⇒ Object



10
11
12
# File 'lib/compass-recipes/sass_extensions.rb', line 10

def value(number)
    Sass::Script::Number.new(number.value, ['']);
end