Sha256: 8342565fc4763766c34f45a9b702ee299675a3f4fb5b0087359abd9bbd9c4eb9

Contents?: true

Size: 1011 Bytes

Versions: 1

Compression:

Stored size: 1011 Bytes

Contents

# Nice little wrapper that lets us easily perform some functions on a
# given color.
class ASE
  class Color
    class << self
      def from_rgba(*args)
        args = args.first if args.length == 1
        self.new *args
      end
      alias :from_rgb :from_rgba

      def from_hex(hex)
        self.new *hex.gsub('#', '').scan(/../).map { |c| c.to_i(16) }
      end
    end

    attr_accessor :r, :g, :b, :a

    # We always internally store the color as RGBA.
    def initialize(r, g, b, a = 255)
      @r = r
      @g = g
      @b = b
      @a = a
    end

    def to_rgba
      [@r, @g, @b, @a]
    end

    def to_rgb
      [@r, @g, @b]
    end

    def to_css
      "rgba(#{r}, #{g}, #{b}, #{a})"
    end

    def [](i)
      [@r, @g, @b, @a][i]
    end

    def to_hex(incl_hash=true, incl_alpha=false)
      hex = incl_hash ? '#' : ''
      colors = [@r, @g, @b]
      colors << @a if incl_alpha

      colors.each { |c| hex << c.to_s(16) }
      return hex
    end
    alias :to_s :to_hex
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ase-1.0.0 lib/ase/color.rb