class NSColor

Public Class Methods

blueControlColor() click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 7
def self.blueControlColor
  NSColor.colorForControlTint(NSBlueControlTint)
end
graphiteControlColor() click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 11
def self.graphiteControlColor
  NSColor.colorForControlTint(NSGraphiteControlTint)
end
rgba(r, g, b, a) click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 32
def self.rgba(r, g, b, a)
  if NSColor.respond_to?('colorWithRed:green:blue:alpha:')
    NSColor.colorWithRed(r, green: g, blue: b, alpha: a)
  else
    NSColor.colorWithCalibratedRed(r, green: g, blue: b, alpha: a)
  end
end
systemControlColor() click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 3
def self.systemControlColor
  NSColor.colorForControlTint(NSColor.currentControlTint)
end

Public Instance Methods

+(color) click to toggle source

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

:white.nscolor + :black.nscolor == :gray.nscolor
# File lib/osx/sugarcube-color/nscolor.rb, line 55
def +(color)
  mix_with(color.nscolor, 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.nscolor << :blue.nscolor == '#ff00ff'.nscolor (:magenta)
:red.nscolor << :blue.nscolor(0.5) == '#ff0080'.nscolor (pinkish)
# File lib/osx/sugarcube-color/nscolor.rb, line 66
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

  NSColor.rgba(r, g, b, a)
end
alpha() click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 174
def alpha
  alphaComponent
rescue Exception
  nil
end
blue() click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 166
def blue
  blueComponent
rescue NSInvalidArgumentException
  whiteComponent
rescue Exception
  nil
end
cgcolor(alpha=nil) click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 44
def cgcolor(alpha=nil)
  nscolor(alpha).CGColor
end
css_name() click to toggle source

returns the closest css name

# File lib/osx/sugarcube-color/nscolor.rb, line 216
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
green() click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 158
def green
  greenComponent
rescue NSInvalidArgumentException
  whiteComponent
rescue Exception
  nil
end
hex() click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 206
def hex
  my_color = self.to_i
  if my_color
    return '#' + my_color.to_s(16).rjust(6, '0')
  else
    nil
  end
end
invert() click to toggle source

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

:white.nscolor.invert == :black.nscolor
# File lib/osx/sugarcube-color/nscolor.rb, line 115
def invert
  r = 1.0 - self.red
  g = 1.0 - self.green
  b = 1.0 - self.blue
  a = self.alpha
  NSColor.rgba(r, g, b, a)
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/osx/sugarcube-color/nscolor.rb, line 78
def mix_with(color, amount)
  color = color.nscolor

  # 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
    NSColor.rgba(r, g, b, a)
  else
    a = (color.alpha - self.alpha) * amount + self.alpha
    return NSColor.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
    NSColor.rgba(r, g, b, a)
  end
end
named_color_space?() click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 40
def named_color_space?
  colorSpaceName == "NSNamedColorSpace"
end
nscolor(alpha=nil) click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 15
def nscolor(alpha=nil)
  if alpha
    if named_color_space?
      color = self.colorUsingColorSpace(NSColorSpace.genericRGBColorSpace)
      if color
        return color.colorWithAlphaComponent(alpha.to_f)
      end
    end
    return self.colorWithAlphaComponent(alpha.to_f)
  elsif named_color_space?
    color = self.colorUsingColorSpace(NSColorSpace.genericRGBColorSpace)
    return color if color
  end

  return self
end
red() click to toggle source

Cannot define method `brightness' because no Objective-C stub was pre-compiled for types `d@:'. Make sure you properly link with the framework or library that defines this message.

def brightness
  brightnessComponent
rescue Exception
  nil
end
# File lib/osx/sugarcube-color/nscolor.rb, line 150
def red
  redComponent
rescue NSInvalidArgumentException
  whiteComponent
rescue Exception
  nil
end
skcolor(alpha=nil) click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 48
def skcolor(alpha=nil)
  nscolor(alpha)
end
system_name() click to toggle source
# File lib/osx/sugarcube-color/nscolor.rb, line 228
def system_name
  system_color = nil
  Symbol.nscolors.each do |color_name, method|
    color = NSColor.send(method)
    without_alpha = self.nscolor(color.alpha)
    if color == self || color == 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/osx/sugarcube-color/nscolor.rb, line 195
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/osx/sugarcube-color/nscolor.rb, line 182
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/osx/sugarcube-to_s/nscolor.rb, line 3
def to_s
  return super unless self.respond_to?(:alpha)

  alpha_s = ((alpha || 1) < 1 ? "(#{alpha})" : '')
  if system_name
    return "NSColor.#{system_name}#{alpha_s.length > 0 ? '.colorWithAlphaComponent' + alpha_s : ''}"
  elsif css_name
    return ":#{css_name}.nscolor#{alpha_s}"
  elsif hex
    return "'#{hex}'.nscolor#{alpha_s}"
  else
    super
  end
end