class SKColor

Public Instance Methods

+(color) click to toggle source

blends two colors by averaging the RGB and alpha components. @example

:white.uicolor + :black.uicolor == :gray.uicolor
# File lib/ios/sugarcube-color/uicolor.rb, line 35
def +(color)
  mix_with(color.uicolor, 0.5)
end
<<(color) click to toggle source

blends two colors by adding the colors, with an upper maximum of 255. Adding white to any color will create white, adding black will do nothing. Also takes transparency into account; adding a transparent color has no effect, adding an opaque color has the most effect. @example

:red.uicolor << :blue.uicolor == '#ff00ff'.uicolor (:magenta)
:red.uicolor << :blue.uicolor(0.5) == '#ff0080'.uicolor (pinkish)
# File lib/ios/sugarcube-color/uicolor.rb, line 46
def <<(color)
  r = [1.0, color.red * color.alpha + self.red].min
  g = [1.0, color.green * color.alpha + self.green].min
  b = [1.0, color.blue * color.alpha + self.blue].min
  a = self.alpha
  UIColor.colorWithRed(r, green:g, blue:b, alpha:a)
end
alpha() click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 126
def alpha
  if @sugarcube_hsb_colors
    @sugarcube_hsb_colors[:alpha]
  elsif _sugarcube_rgb_colors
    _sugarcube_rgb_colors[:alpha]
  end
end
blue() click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 122
def blue
  _sugarcube_rgb_colors && _sugarcube_rgb_colors[:blue]
end
brightness() click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 110
def brightness
  _sugarcube_hsb_colors && _sugarcube_hsb_colors[:brightness]
end
cgcolor(alpha=nil) click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 11
def cgcolor(alpha=nil)
  uicolor(alpha).CGColor
end
change_brightness(amount) click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 27
def change_brightness(amount)
  new_brightness = brightness + amount
  UIColor.colorWithHue(hue, saturation: saturation, brightness: new_brightness, alpha: alpha)
end
cicolor() click to toggle source
# File lib/ios/sugarcube-image/uicolor.rb, line 3
def cicolor
  self.CIColor
end
css_name() click to toggle source

returns the closest css name

# File lib/ios/sugarcube-color/uicolor.rb, line 170
def css_name
  my_color = self.to_i
  css_name = nil
  Symbol.css_colors.each do |color, hex|
    if hex == my_color
      css_name = color
      break
    end
  end
  return css_name
end
darken(amount=0.1) click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 19
def darken(amount=0.1)
  change_brightness -amount
end
green() click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 118
def green
  _sugarcube_rgb_colors && _sugarcube_rgb_colors[:green]
end
hex() click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 160
def hex
  my_color = self.to_i
  if my_color
    return '#' + my_color.to_s(16).rjust(6, '0')
  else
    nil
  end
end
hue() click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 102
def hue
  _sugarcube_hsb_colors && _sugarcube_hsb_colors[:hue]
end
invert() click to toggle source

inverts the RGB channel. keeps the alpha channel unchanged @example

:white.uicolor.invert == :black.uicolor
# File lib/ios/sugarcube-color/uicolor.rb, line 94
def invert
  r = 1.0 - self.red
  g = 1.0 - self.green
  b = 1.0 - self.blue
  a = self.alpha
  UIColor.colorWithRed(r, green:g, blue:b, alpha:a)
end
lighten(amount=0.1) click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 23
def lighten(amount=0.1)
  change_brightness amount
end
mix_with(color, amount) click to toggle source

a more generic color mixing method. mixes two colors, but a second parameter determines how much of each. 0.5 means equal parts, 0.0 means use all of the first, and 1.0 means use all of the second

# File lib/ios/sugarcube-color/uicolor.rb, line 57
def mix_with(color, amount)
  color = color.uicolor

  # make amount between 0 and 1
  amount = [[0, amount].max, 1].min
  # start with precise amounts: 0, 0.5, and 1.
  if amount == 0 && self.alpha == color.alpha
    self
  elsif amount == 1 && self.alpha == color.alpha
    color
  elsif amount == 0.5 && self.alpha == color.alpha
    r = (self.red + color.red) / 2
    g = (self.green + color.green) / 2
    b = (self.blue + color.blue) / 2
    a = self.alpha
    UIColor.colorWithRed(r, green:g, blue:b, alpha:a)
  else
    a = (color.alpha - self.alpha) * amount + self.alpha
    return UIColor.clearColor if a == 0

    color_red = color.red * color.alpha + self.red * (1 - color.alpha)
    self_red = self.red * self.alpha + color.red * (1 - self.alpha)
    color_green = color.green * color.alpha + self.green * (1 - color.alpha)
    self_green = self.green * self.alpha + color.green * (1 - self.alpha)
    color_blue = color.blue * color.alpha + self.blue * (1 - color.alpha)
    self_blue = self.blue * self.alpha + color.blue * (1 - self.alpha)

    r = (color_red - self_red) * amount + self_red
    g = (color_green - self_green) * amount + self_green
    b = (color_blue - self_blue) * amount + self_blue
    UIColor.colorWithRed(r, green:g, blue:b, alpha:a)
  end
end
red() click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 114
def red
  _sugarcube_rgb_colors && _sugarcube_rgb_colors[:red]
end
saturation() click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 106
def saturation
  _sugarcube_hsb_colors && _sugarcube_hsb_colors[:saturation]
end
skcolor(alpha=nil) click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 15
def skcolor(alpha=nil)
  uicolor(alpha)
end
system_name() click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 182
def system_name
  system_color = nil
  without_alpha = self.uicolor(1)
  Symbol.uicolors.each do |color, method|
    if UIColor.send(method) == without_alpha
      system_color = method
      break
    end
  end
  return system_color
end
to_a() click to toggle source

returns the components as an array of 32 bit RGB values. alpha channel is dropped

# File lib/ios/sugarcube-color/uicolor.rb, line 149
def to_a
  if self.red && self.green && self.blue
    red = (self.red * 255).round
    green = (self.green * 255).round
    blue = (self.blue * 255).round
    return [red, green, blue]
  else
    return nil
  end
end
to_i() click to toggle source

returns the components OR'd together, as 32 bit RGB integer. alpha channel is dropped

# File lib/ios/sugarcube-color/uicolor.rb, line 136
def to_i
  if self.red && self.green && self.blue
    red = (self.red * 255).round << 16
    green = (self.green * 255).round << 8
    blue = (self.blue * 255).round
    return red + green + blue
  else
    return nil
  end
end
to_s() click to toggle source
Calls superclass method
# File lib/ios/sugarcube-to_s/uicolor.rb, line 3
def to_s
  return super unless self.respond_to?(:alpha)

  alpha_s = ((alpha || 1) < 1 ? "(#{alpha})" : '')
  if system_name
    return "UIColor.#{system_name}#{alpha_s.length > 0 ? '.colorWithAlphaComponent' + alpha_s : ''}"
  elsif css_name
    return ":#{css_name}.uicolor#{alpha_s}"
  elsif hex
    return "'#{hex}'.uicolor#{alpha_s}"
  else
    super
  end
end
uicolor(alpha=nil) click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 3
def uicolor(alpha=nil)
  if alpha
    self.colorWithAlphaComponent(alpha.to_f)
  else
    self
  end
end

Private Instance Methods

_sugarcube_hsb_colors() click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 195
def _sugarcube_hsb_colors
  @sugarcube_hsb_colors ||= begin
    type = CGSize.type[/(f|d)/]
    hue = Pointer.new(type)
    saturation = Pointer.new(type)
    brightness = Pointer.new(type)
    alpha = Pointer.new(type)
    white = Pointer.new(type)

    if self.getHue(hue, saturation:saturation, brightness:brightness, alpha:alpha)
      {
        hue: hue[0],
        saturation: saturation[0],
        brightness: brightness[0],
        alpha: alpha[0],
      }
    elsif self.getWhite(white, alpha:alpha)
      {
        hue: 0,
        saturation: 0,
        brightness: white[0],
        alpha: alpha[0],
      }
    else
      nil
    end
  end
end
_sugarcube_rgb_colors() click to toggle source
# File lib/ios/sugarcube-color/uicolor.rb, line 224
def _sugarcube_rgb_colors
  @sugarcube_rgb_colors ||= begin
    type = CGSize.type[/(f|d)/]
    red = Pointer.new(type)
    green = Pointer.new(type)
    blue = Pointer.new(type)
    alpha = Pointer.new(type)
    white = Pointer.new(type)
    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