Class: NSColor

Inherits:
Object show all
Defined in:
lib/osx/sugarcube-to_s/nscolor.rb,
lib/osx/sugarcube-color/nscolor.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.blueControlColorObject



7
8
9
# File 'lib/osx/sugarcube-color/nscolor.rb', line 7

def self.blueControlColor
  NSColor.colorForControlTint(NSBlueControlTint)
end

.graphiteControlColorObject



11
12
13
# File 'lib/osx/sugarcube-color/nscolor.rb', line 11

def self.graphiteControlColor
  NSColor.colorForControlTint(NSGraphiteControlTint)
end

.systemControlColorObject



3
4
5
# File 'lib/osx/sugarcube-color/nscolor.rb', line 3

def self.systemControlColor
  NSColor.colorForControlTint(NSColor.currentControlTint)
end

Instance Method Details

#+(color) ⇒ Object

blends two colors by averaging the RGB and alpha components.

Examples:

:white.nscolor + :black.nscolor == :gray.nscolor


46
47
48
# File 'lib/osx/sugarcube-color/nscolor.rb', line 46

def +(color)
  mix_with(color.nscolor, 0.5)
end

#<<(color) ⇒ Object

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.

Examples:

:red.nscolor << :blue.nscolor == '#ff00ff'.nscolor (:magenta)
:red.nscolor << :blue.nscolor(0.5) == '#ff0080'.nscolor (pinkish)


57
58
59
60
61
62
63
# File 'lib/osx/sugarcube-color/nscolor.rb', line 57

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.colorWithRed(r, green:g, blue:b, alpha:a)
end

#alphaObject



164
165
166
167
168
# File 'lib/osx/sugarcube-color/nscolor.rb', line 164

def alpha
  alphaComponent
rescue Exception
  nil
end

#blueObject



156
157
158
159
160
161
162
# File 'lib/osx/sugarcube-color/nscolor.rb', line 156

def blue
  blueComponent
rescue NSInvalidArgumentException
  whiteComponent
rescue Exception
  nil
end

#cgcolor(alpha = nil) ⇒ Object



35
36
37
# File 'lib/osx/sugarcube-color/nscolor.rb', line 35

def cgcolor(alpha=nil)
  nscolor(alpha).CGColor
end

#css_nameObject

returns the closest css name



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/osx/sugarcube-color/nscolor.rb', line 206

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

#greenObject



148
149
150
151
152
153
154
# File 'lib/osx/sugarcube-color/nscolor.rb', line 148

def green
  greenComponent
rescue NSInvalidArgumentException
  whiteComponent
rescue Exception
  nil
end

#hexObject



196
197
198
199
200
201
202
203
# File 'lib/osx/sugarcube-color/nscolor.rb', line 196

def hex
  my_color = self.to_i
  if my_color
    return '#' + my_color.to_s(16).rjust(6, '0')
  else
    nil
  end
end

#invertObject

inverts the RGB channel. keeps the alpha channel unchanged

Examples:

:white.nscolor.invert == :black.nscolor


105
106
107
108
109
110
111
# File 'lib/osx/sugarcube-color/nscolor.rb', line 105

def invert
  r = 1.0 - self.red
  g = 1.0 - self.green
  b = 1.0 - self.blue
  a = self.alpha
  NSColor.colorWithRed(r, green: g, blue: b, alpha: a)
end

#mix_with(color, amount) ⇒ Object

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/osx/sugarcube-color/nscolor.rb', line 68

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.colorWithRed(r, green:g, blue:b, alpha: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.colorWithRed(r, green:g, blue:b, alpha:a)
  end
end

#named_color_space?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/osx/sugarcube-color/nscolor.rb', line 31

def named_color_space?
  colorSpaceName == "NSNamedColorSpace"
end

#nscolor(alpha = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/osx/sugarcube-color/nscolor.rb', line 15

def nscolor(alpha=nil)
  if alpha
    if named_color_space?
      self.colorUsingColorSpace(NSColorSpace.genericRGBColorSpace).colorWithAlphaComponent(alpha.to_f)
    else
      self.colorWithAlphaComponent(alpha.to_f)
    end
  else
    if named_color_space?
      self.colorUsingColorSpace(NSColorSpace.genericRGBColorSpace)
    else
      self
    end
  end
end

#redObject

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



140
141
142
143
144
145
146
# File 'lib/osx/sugarcube-color/nscolor.rb', line 140

def red
  redComponent
rescue NSInvalidArgumentException
  whiteComponent
rescue Exception
  nil
end

#skcolor(alpha = nil) ⇒ Object



39
40
41
# File 'lib/osx/sugarcube-color/nscolor.rb', line 39

def skcolor(alpha=nil)
  nscolor(alpha)
end

#system_nameObject



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/osx/sugarcube-color/nscolor.rb', line 218

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_aObject

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



185
186
187
188
189
190
191
192
193
194
# File 'lib/osx/sugarcube-color/nscolor.rb', line 185

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_iObject

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



172
173
174
175
176
177
178
179
180
181
# File 'lib/osx/sugarcube-color/nscolor.rb', line 172

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_sObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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