Class: Picss::CSSWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/picss/css_writer.rb

Constant Summary collapse

@@template =
<<-EOS
.class {
  position: relative;
  height: {{height}}px;
  width: {{width}}px;
}
.class:after {
  content: " ";
  position: absolute;
  background: transparent;
  height: {{scale}}px;
  width: {{scale}}px;
  top: -{{scale}}px;
  left: -{{scale}}px;
  box-shadow:
{{box_shadows}}
  ;
}
EOS
@@pixel_template =
'    {{x}}px {{y}}px 0 0 {{color}}'

Class Method Summary collapse

Class Method Details

.convert(image, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/picss/css_writer.rb', line 27

def self.convert(image, options={})
  box_shadows = Array.new
  image.height.times do |y|
    image.height.times do |x|
      pixel = image.get_pixel(x, y)
      if pixel != 0
        box_shadows.push(Mustache.render @@pixel_template, {
          x: x + options[:scale],
          y: y + options[:scale],
          color: ColorConverter.hex_to_rgba(pixel.to_s(16).rjust(8, '0'))
          })
      end
    end
  end
  options[:box_shadows] = box_shadows.join(",\n")
  options[:height] = image.height
  options[:width] = image.width
  Mustache.render @@template, options
end