Class: UIColor
- Defined in:
- lib/sugarcube-to_s/uicolor.rb,
lib/sugarcube-color/uicolor.rb,
lib/sugarcube-image/uicolor.rb
Instance Method Summary collapse
-
#+(color) ⇒ Object
blends two colors by averaging the RGB and alpha components.
-
#<<(color) ⇒ Object
blends two colors by adding the colors, with an upper maximum of 255.
- #alpha ⇒ Object
- #blue ⇒ Object
- #brightness ⇒ Object
- #cgcolor ⇒ Object
- #cicolor ⇒ Object
-
#css_name ⇒ Object
returns the closest css name.
- #green ⇒ Object
- #hex ⇒ Object
- #hue ⇒ Object
- #inspect ⇒ Object
-
#invert ⇒ Object
inverts the RGB channel.
-
#mix_with(color, amount) ⇒ Object
a more generic color mixing method.
- #red ⇒ Object
- #saturation ⇒ Object
- #system_name ⇒ Object
-
#to_a ⇒ Object
returns the components as an array of 32 bit RGB values.
-
#to_i ⇒ Object
returns the components OR'd together, as 32 bit RGB integer.
- #to_s ⇒ Object
- #uicolor(alpha = nil) ⇒ Object
Instance Method Details
#+(color) ⇒ Object
blends two colors by averaging the RGB and alpha components.
18 19 20 |
# File 'lib/sugarcube-color/uicolor.rb', line 18 def +(color) mix_with(color.uicolor, 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.
29 30 31 32 33 34 35 |
# File 'lib/sugarcube-color/uicolor.rb', line 29 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 ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/sugarcube-color/uicolor.rb', line 109 def alpha if @sugarcube_hsb_colors @sugarcube_hsb_colors[:alpha] elsif _sugarcube_rgb_colors _sugarcube_rgb_colors[:alpha] end end |
#blue ⇒ Object
105 106 107 |
# File 'lib/sugarcube-color/uicolor.rb', line 105 def blue _sugarcube_rgb_colors && _sugarcube_rgb_colors[:blue] end |
#brightness ⇒ Object
93 94 95 |
# File 'lib/sugarcube-color/uicolor.rb', line 93 def brightness _sugarcube_hsb_colors && _sugarcube_hsb_colors[:brightness] end |
#cgcolor ⇒ Object
11 12 13 |
# File 'lib/sugarcube-color/uicolor.rb', line 11 def cgcolor self.CGColor end |
#cicolor ⇒ Object
3 4 5 |
# File 'lib/sugarcube-image/uicolor.rb', line 3 def cicolor self.CIColor end |
#css_name ⇒ Object
returns the closest css name
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/sugarcube-color/uicolor.rb', line 153 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 ⇒ Object
101 102 103 |
# File 'lib/sugarcube-color/uicolor.rb', line 101 def green _sugarcube_rgb_colors && _sugarcube_rgb_colors[:green] end |
#hex ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/sugarcube-color/uicolor.rb', line 143 def hex my_color = self.to_i if my_color return '#' + my_color.to_s(16).rjust(6, '0') else nil end end |
#hue ⇒ Object
85 86 87 |
# File 'lib/sugarcube-color/uicolor.rb', line 85 def hue _sugarcube_hsb_colors && _sugarcube_hsb_colors[:hue] end |
#inspect ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/sugarcube-to_s/uicolor.rb', line 19 def inspect return super unless self.respond_to?(:alpha) alpha_s = ((alpha || 1) < 1 ? "(#{alpha})" : '') if system_name return "UIColor.#{system_name}#{alpha_s}" elsif css_name return ":#{css_name}.uicolor#{alpha_s}" elsif hex return "'#{hex}'.uicolor#{alpha_s}" else super end end |
#invert ⇒ Object
inverts the RGB channel. keeps the alpha channel unchanged
77 78 79 80 81 82 83 |
# File 'lib/sugarcube-color/uicolor.rb', line 77 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 |
#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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/sugarcube-color/uicolor.rb', line 40 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 ⇒ Object
97 98 99 |
# File 'lib/sugarcube-color/uicolor.rb', line 97 def red _sugarcube_rgb_colors && _sugarcube_rgb_colors[:red] end |
#saturation ⇒ Object
89 90 91 |
# File 'lib/sugarcube-color/uicolor.rb', line 89 def saturation _sugarcube_hsb_colors && _sugarcube_hsb_colors[:saturation] end |
#system_name ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/sugarcube-color/uicolor.rb', line 165 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 ⇒ Object
returns the components as an array of 32 bit RGB values. alpha channel is dropped
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/sugarcube-color/uicolor.rb', line 132 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 ⇒ Object
returns the components OR'd together, as 32 bit RGB integer. alpha channel is dropped
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/sugarcube-color/uicolor.rb', line 119 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 ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/sugarcube-to_s/uicolor.rb', line 3 def to_s return super unless self.respond_to?(:alpha) alpha_s = ((alpha || 1) < 1 ? "(#{alpha})" : '') system_color = system_name return "UIColor.#{system_color}#{alpha_s}" if system_color alpha_s = ((alpha || 1) < 1 ? ", alpha: #{alpha}" : '') inside = (css_name && ":#{css_name}") || (hex && "'#{hex}'") if inside return "UIColor.color(#{inside}#{alpha_s})" else super end end |
#uicolor(alpha = nil) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/sugarcube-color/uicolor.rb', line 3 def uicolor(alpha=nil) if alpha self.colorWithAlphaComponent(alpha.to_f) else self end end |