class DYI::Color

@since 0.0.0

Attributes

name[R]

Public Class Methods

check_format(fmt) click to toggle source
# File lib/dyi/color.rb, line 196
def check_format(fmt)
  begin
    (fmt = fmt.to_s) % [0.0, 0.0, 0.0]
    fmt
  rescue
    raise ArgumentError, "wrong format: `#{fmt}'"
  end
end
default_format() click to toggle source
# File lib/dyi/color.rb, line 192
def default_format
  @@default_format
end
method_missing(name, *args) click to toggle source
Calls superclass method
# File lib/dyi/color.rb, line 171
def method_missing(name, *args)
  if args.size == 0 && color = named_color(name)
    instance_eval %{
      def self.#{name}
        @#{name}
      end
    }
    return color
  end
  super
end
new (color_string) click to toggle source
new (rgb_array)
new (rgb_hash)
new (red, green, blue)
new (color)
# File lib/dyi/color.rb, line 36
def initialize(*args)
  case args.size
    when 1 then color = args.first
    when 3 then color = args
    else raise ArgumentError, "wrong number of arguments (#{args.size} for #{args.size == 0 ? 1 : 3})"
  end

  case color
  when Color
    @r = color.red
    @g = color.green
    @b = color.blue
    @name = color.name
  when Array
    raise ArgumentError, "illegal size of Array: #{color.size}" if color.size != 3
    @r, @g, @b = color.map {|c| c.to_i & 0xff}
    @name = nil
  when Hash
    @r, @g, @b = [:r, :g, :b].map {|key| color[key].to_i & 0xff}
    @name = nil
  when String, Symbol
    color = color.to_s
    if color =~ /^\s*#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})\s*$/ # #ffffff
      @r, @g, @b = [$1, $2, $3].map {|s| s.hex}
      @name = nil
    elsif color =~ /^\s*#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])\s*$/ # #fff
      @r, @g, @b = [$1, $2, $3].map {|s| (s * 2).hex}
      @name = nil
    elsif color =~ /^\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*$/ # rgb(255,255,255)
      @r, @g, @b = [$1, $2, $3].map {|s| s.to_i & 0xff}
      @name = nil
    elsif color =~ /^\s*rgb\s*\(\s*(\d+)%\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)\s*$/ # rgb(100%,100%,100%)
      @r, @g, @b = [$1, $2, $3].map {|s| (0xff * s.to_f / 100).to_i & 0xff}
      @name = nil
    else
      raise ArgumentError, "argument is empty" if color.size == 0
      raise ArgumentError, "`#{color}' is unknown color" unless rgb = @@named_colors[color.downcase]
      @r, @g, @b = rgb
      @name = color.downcase
    end
  else
    raise TypeError, "#{color.class} can't be coerced into #{self.class}"
  end
end
new(*args) click to toggle source
Calls superclass method
# File lib/dyi/color.rb, line 153
def new(*args)
  if args.size == 1
    case args.first
    when self
      return args.first
    when String, Symbol
      if color = named_color(args.first)
        return color
      end
    end
  end
  super
end
new_or_nil(*args) click to toggle source
# File lib/dyi/color.rb, line 167
def new_or_nil(*args)
  (args.size == 1 && args.first.nil?) ? nil : new(*args)
end
set_default_format(fmt, disp_ratio=false) { || ... } click to toggle source
# File lib/dyi/color.rb, line 183
def set_default_format(fmt, disp_ratio=false)
  org_format = @@default_format
  @@default_format = [check_format(fmt), disp_ratio]
  if block_given?
    yield
    @@default_format = org_format
  end
end

Private Class Methods

named_color(name) click to toggle source
# File lib/dyi/color.rb, line 207
def named_color(name)
  name = name.to_s.downcase
  color = instance_variable_get('@' + name) rescue (return nil)
  return color if color
  if @@named_colors[name]
    color = allocate
    color.__send__(:initialize, name)
    instance_variable_set('@' + name, color)
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/dyi/color.rb, line 81
def ==(other)
  return false unless other.instance_of?(self.class)
  @r == other.red && @g == other.green && @b = other.blue
end
blue() click to toggle source
# File lib/dyi/color.rb, line 107
def blue
  @b
end
color?() click to toggle source
# File lib/dyi/color.rb, line 132
def color?
  true
end
create_cls_brush(opacity=nil, shape=nil) click to toggle source
# File lib/ironruby.rb, line 61
def create_cls_brush(opacity=nil, shape=nil)
  System::Drawing::SolidBrush.new(to_cls_color(opacity))
end
create_cls_pen(opacity=nil) click to toggle source
# File lib/ironruby.rb, line 57
def create_cls_pen(opacity=nil)
  System::Drawing::Pen.new(to_cls_color(opacity))
end
eql?(other) click to toggle source
# File lib/dyi/color.rb, line 86
def eql?(other)
  return false unless self.class == other.class
  self == other
end
green() click to toggle source
# File lib/dyi/color.rb, line 103
def green
  @g
end
hash() click to toggle source
# File lib/dyi/color.rb, line 91
def hash
  (@r << 16) + (@g << 8) + @b 
end
merge(other, ratio) click to toggle source
# File lib/dyi/color.rb, line 123
def merge(other, ratio)
  raise ArgumentError, "ratio should be number between 0 and 1" if ratio < 0 || 1 < ratio
  other = self.class.new(other)
  r = @r * (1.0 - ratio) + other.red * ratio
  g = @g * (1.0 - ratio) + other.green * ratio
  b = @b * (1.0 - ratio) + other.blue * ratio
  self.class.new(r,g,b)
end
named?() click to toggle source
# File lib/dyi/color.rb, line 95
def named?
  not @name.nil?
end
r_blue() click to toggle source
# File lib/dyi/color.rb, line 119
def r_blue
  @b.quo(0.255).floor.quo(1000.0)
end
r_green() click to toggle source
# File lib/dyi/color.rb, line 115
def r_green
  @g.quo(0.255).floor.quo(1000.0)
end
r_red() click to toggle source
# File lib/dyi/color.rb, line 111
def r_red
  @r.quo(0.255).floor.quo(1000.0)
end
red() click to toggle source
# File lib/dyi/color.rb, line 99
def red
  @r
end
to_cls_color(opacity=nil) click to toggle source
# File lib/ironruby.rb, line 49
def to_cls_color(opacity=nil)
  if opacity
    System::Drawing::Color.from_argb((opacity.to_f * 255).to_i, @r, @g, @b)
  else
    System::Drawing::Color.from_argb(@r, @g, @b)
  end
end
to_s () click to toggle source
to_s (fmt, disp_ratio=false)
# File lib/dyi/color.rb, line 139
def to_s(*args)
  args[0] = self.class.check_format(args[0]) if args[0]
  args = @@default_format if args.empty?
  args[0] % (args[1] ? [r_red, r_green, r_blue] : [@r,@g,@b])
end
to_s16(opacity=nil) click to toggle source
# File lib/dyi/color.rb, line 145
def to_s16(opacity=nil)
  opacity ? '#%02X%02X%02X%02X' % [0xff && (0xff * opacity), @r,@g,@b] : to_s('#%02X%02X%02X')
end