Sha256: f0dd6a599ed1c3e6629ad43a98be6cee49ab4a24416a4444d40dfbd58f69f3c2

Contents?: true

Size: 1.45 KB

Versions: 16

Compression:

Stored size: 1.45 KB

Contents

class UIColor
  def uicolor(alpha=nil)
    if alpha
      self.colorWithAlphaComponent(alpha.to_f)
    else
      self
    end
  end

  def cgcolor
    self.CGColor
  end

  def red
    _sugarcube_colors && _sugarcube_colors[:red]
  end

  def green
    _sugarcube_colors && _sugarcube_colors[:green]
  end

  def blue
    _sugarcube_colors && _sugarcube_colors[:blue]
  end

  def alpha
    _sugarcube_colors && _sugarcube_colors[:alpha]
  end

  # returns the closest css name
  def css_name
    found = Symbol.css_colors.map { |key, val| [key, _sugarcube_color_compare(self, val.uicolor)] }.inject{|c1,c2| c1[1] < c2[1] ? c1 : c2 }
    threshold = 0.03
    if found[1] > 0.03
      nil
    else
      found[0]
    end
  end

private
  def _sugarcube_color_compare(c1, c2)
    return (c1.red - c2.red).abs + (c1.green - c2.green).abs + (c1.blue - c2.blue).abs
  end

  def _sugarcube_colors
    @color ||= begin
      red = Pointer.new(:float)
      green = Pointer.new(:float)
      blue = Pointer.new(:float)
      white = Pointer.new(:float)
      alpha = Pointer.new(:float)
      if self.getRed(red, green:green, blue:blue, alpha:alpha)
        {
          red: red[0],
          green: green[0],
          blue: blue[0],
          alpha: alpha[0],
        }
      elsif self.getWhite(white, alpha:alpha)
        {
          red: white[0],
          green: white[0],
          blue: white[0],
          alpha: alpha[0],
        }
      else
        nil
      end
    end
  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
sugarcube-0.18.16 lib/sugarcube/uicolor.rb
sugarcube-0.18.12 lib/sugarcube/uicolor.rb
sugarcube-0.18.11 lib/sugarcube/uicolor.rb
sugarcube-0.18.10 lib/sugarcube/uicolor.rb
sugarcube-0.18.9 lib/sugarcube/uicolor.rb
sugarcube-0.18.8 lib/sugarcube/uicolor.rb
sugarcube-0.18.7 lib/sugarcube/uicolor.rb
sugarcube-0.18.6 lib/sugarcube/uicolor.rb
sugarcube-0.18.5 lib/sugarcube/uicolor.rb
sugarcube-0.18.4 lib/sugarcube/uicolor.rb
sugarcube-0.18.3 lib/sugarcube/uicolor.rb
sugarcube-0.18.2 lib/sugarcube/uicolor.rb
sugarcube-0.18.1 lib/sugarcube/uicolor.rb
sugarcube-0.18.0 lib/sugarcube/uicolor.rb
sugarcube-0.16.9 lib/sugarcube/uicolor.rb
sugarcube-0.16.5 lib/sugarcube/uicolor.rb