1 | /*-------------------------------------------------------------------------------------------------------*\ |
---|
2 | | Adium, Copyright (C) 2001-2005, Adam Iser (adamiser@mac.com | http://www.adiumx.com) | |
---|
3 | \---------------------------------------------------------------------------------------------------------/ |
---|
4 | | This program is free software; you can redistribute it and/or modify it under the terms of the GNU |
---|
5 | | General Public License as published by the Free Software Foundation; either version 2 of the License, |
---|
6 | | or (at your option) any later version. |
---|
7 | | |
---|
8 | | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
---|
9 | | the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
10 | | Public License for more details. |
---|
11 | | |
---|
12 | | You should have received a copy of the GNU General Public License along with this program; if not, |
---|
13 | | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
14 | \------------------------------------------------------------------------------------------------------ */ |
---|
15 | |
---|
16 | @interface NSDictionary (AIColorAdditions_RGBTxtFiles) |
---|
17 | |
---|
18 | /*! @brief Loads an emacs-style rgb.txt file as an \c NSDictionary. |
---|
19 | * |
---|
20 | * @par See /usr/share/emacs/(some version)/etc/rgb.txt for an example of such a file. |
---|
21 | * |
---|
22 | * @par The pathname does not need to end in 'rgb.txt', but it must be a file in UTF-8 encoding. |
---|
23 | * |
---|
24 | * @par The keys in the dictionary are the color names from the file, <em>converted to lowercase</em>. |
---|
25 | * |
---|
26 | * @param path The pathname of the file to read. |
---|
27 | * @return A dictionary mapping color names to RGB <code>NSColor</code>s. |
---|
28 | */ |
---|
29 | + (id)dictionaryWithContentsOfRGBTxtFile:(NSString *)path; |
---|
30 | |
---|
31 | @end |
---|
32 | |
---|
33 | @interface NSColor (AIColorAdditions_RGBTxtFiles) |
---|
34 | |
---|
35 | /*! @brief Loads the rgb.txt file from the system installation of emacs and returns a dictionary created from it. |
---|
36 | * |
---|
37 | * @par This method loads the file at /usr/share/emacs/ * /etc/rgb.txt. It is not guaranteed to load any specific file matching that glob, but it will load only one. |
---|
38 | * |
---|
39 | * @return A dictionary mapping color names (all of which are lowercase) to RGB <code>NSColor</code>s. |
---|
40 | */ |
---|
41 | + (NSDictionary *)colorNamesDictionary; |
---|
42 | |
---|
43 | @end |
---|
44 | |
---|
45 | @interface NSColor (AIColorAdditions_Comparison) |
---|
46 | |
---|
47 | /*! @brief Returns whether two RGB colors are equal. |
---|
48 | * |
---|
49 | * @par This method assumes that the receiver and the other color are both in an RGB color-space. If they're not, you will get an exception. |
---|
50 | * |
---|
51 | * @par The alpha component is considered in the comparison. |
---|
52 | * |
---|
53 | * @return \c YES if the two colors are equal; \c NO if they're not. |
---|
54 | */ |
---|
55 | - (BOOL)equalToRGBColor:(NSColor *)inColor; |
---|
56 | |
---|
57 | @end |
---|
58 | |
---|
59 | @interface NSColor (AIColorAdditions_DarknessAndContrast) |
---|
60 | |
---|
61 | /*! @brief Returns whether a color is dark. |
---|
62 | * |
---|
63 | * @par This method measures the brightness of the receiver using <code>-[NSColor brightnessComponent]</code>, and considers it dark if that value < 0.5. |
---|
64 | * |
---|
65 | * @par A color whose brightness is exactly 0.5 is not considered dark. |
---|
66 | * |
---|
67 | * @return \c YES if the receiver is dark; NO if it isn't. |
---|
68 | */ |
---|
69 | - (BOOL)colorIsDark; |
---|
70 | /*! @brief Returns whether a color is medium in brightness. |
---|
71 | * |
---|
72 | * @par This method measures the brightness of the receiver using <code>-[NSColor brightnessComponent]</code>, and considers it medium if 0.35 < that value < 0.65. |
---|
73 | * |
---|
74 | * @par A color whose brightness is exactly 0.35 or 0.65 is not considered medium. |
---|
75 | * |
---|
76 | * @return \c YES if the receiver is medium in brightness; NO if it isn't. |
---|
77 | */ |
---|
78 | - (BOOL)colorIsMedium; |
---|
79 | /*! @brief Returns a new color, darker than the receiver by a fixed amount. |
---|
80 | * |
---|
81 | * @par This method subtracts \a amount from the brightness of the receiver (as measured using <code>-[NSColor brightnessComponent]</code>), and returns a new \c NSColor created with the unchanged other component values and the new brightness value. |
---|
82 | * |
---|
83 | * @par This method does not adjust the saturation of the receiver. For a method that does, see <code>darkenAndAdjustSaturationBy:</code>. |
---|
84 | * |
---|
85 | * @param amount The amount (greater than 0.0 and at most 1.0) to subtract from the receiver's brightness. |
---|
86 | * @return A new, darker color, in the calibrated-RGB color space. |
---|
87 | */ |
---|
88 | - (NSColor *)darkenBy:(float)amount; |
---|
89 | /*! @brief Returns a new color, darker than the receiver and more saturated, both by a single fixed amount. |
---|
90 | * |
---|
91 | * @par This method subtracts \a amount from the brightness of the receiver (as measured using <code>-[NSColor brightnessComponent]</code>) and adds \a amount to its saturation (as measured using <code>-[NSColor saturationComponent]</code>), and returns a new \c NSColor created with the unchanged other component values and the new saturation and brightness values. |
---|
92 | * |
---|
93 | * @par This method increases the saturation of the receiver. For a method that doesn't, see <code>darkenBy:</code>. |
---|
94 | * |
---|
95 | * @par You would use this method when you want to darken a color while preserving the âcolorâ apparent to the user. The usual reason to do this is to create two ends of a gradient from a single input color. |
---|
96 | * |
---|
97 | * @param amount The amount (greater than 0.0 and at most 1.0) to subtract from the receiver's brightness and add to the receiver's saturation. |
---|
98 | * @return A new, darker, more saturated color, in the calibrated-RGB color space. |
---|
99 | */ |
---|
100 | - (NSColor *)darkenAndAdjustSaturationBy:(float)amount; |
---|
101 | /*! @brief Inverts a color's brightness without affecting the hue. |
---|
102 | * |
---|
103 | * @par The naïve method for inverting a color is to subtract all of its color components from 1.0, but this moves colors not in the center of the color wheel to the opposite side of the wheelâfor example, it changes orange into light blue. In other words, it inverts not the brightness (except for shades of gray), but the hue. |
---|
104 | * |
---|
105 | * @par This method, on the other hand, inverts the brightness of a color without moving it across the color wheel. For example, dark orange becomes light orange, and vice versa. |
---|
106 | * |
---|
107 | * @return The inverted color, in the calibrated-RGB color space. |
---|
108 | */ |
---|
109 | - (NSColor *)colorWithInvertedLuminance; |
---|
110 | /*! @brief Returns a color that contrasts well with the receiver. |
---|
111 | * |
---|
112 | * @par If the receiver is medium (see \c colorIsMedium), this method returns white if the receiver is dark (see \c colorIsDark), or black if it isn't. If the receiver is not medium, this method inverts the receiver and returns the result. |
---|
113 | * |
---|
114 | * @return A new color. |
---|
115 | */ |
---|
116 | - (NSColor *)contrastingColor; |
---|
117 | |
---|
118 | @end |
---|
119 | |
---|
120 | /*! @brief Computes the hue, saturation, and brightness of a given red, green, and blue triplet. |
---|
121 | * |
---|
122 | * @par This function exists to help you compute HSL values without first creating an \c NSColor instance. It also gets the job done in only one function call, rather than at least two Objective-C messages. |
---|
123 | * |
---|
124 | * @par Hue is expressed as a fraction of a circle. Red is any multiple of 1 (a whole circle), and will be returned as 0. |
---|
125 | * |
---|
126 | * @param hue A pointer to a \c float. If you don't pass \c NULL, this function will assign the hue component here. |
---|
127 | * @param saturation A pointer to a \c float. If you don't pass \c NULL, this function will assign the saturation component here. |
---|
128 | * @param luminance A pointer to a \c float. If you don't pass \c NULL, this function will assign the luminance (brightness) component here. |
---|
129 | * @param r The red component of the color to convert. |
---|
130 | * @param g The green component of the color to convert. |
---|
131 | * @param b The blue component of the color to convert. |
---|
132 | */ |
---|
133 | void getHueLuminanceSaturationFromRGB(float *hue, float *luminance, float *saturation, float r, float g, float b); |
---|
134 | /*! @brief Computes the red, green, and blue of a given hue, saturation, and brightness triplet. |
---|
135 | * |
---|
136 | * @par This function exists to help you compute RGB values without first creating an \c NSColor instance. It also gets the job done in only one function call, rather than at least two Objective-C messages. |
---|
137 | * |
---|
138 | * @par Hue is expressed as a fraction of a circle. Red is any multiple of 1 (a whole circle), such as 0. |
---|
139 | * |
---|
140 | * @param r A pointer to a \c float. If you don't pass \c NULL, this function will assign the red component here. |
---|
141 | * @param g A pointer to a \c float. If you don't pass \c NULL, this function will assign the green component here. |
---|
142 | * @param b A pointer to a \c float. If you don't pass \c NULL, this function will assign the blue component here. |
---|
143 | * @param hue The hue component of the color to convert. |
---|
144 | * @param saturation The saturation component of the color to convert. |
---|
145 | * @param luminance The luminance (brightness) component of the color to convert. |
---|
146 | */ |
---|
147 | void getRGBFromHueLuminanceSaturation(float *r, float *g, float *b, float hue, float luminance, float saturation); |
---|
148 | |
---|
149 | @interface NSColor (AIColorAdditions_HLS) |
---|
150 | |
---|
151 | /*! @brief Returns a new color with hue, saturation, and brightness adjusted by given deltas. |
---|
152 | * |
---|
153 | * @par Each of the deltas will be added to that component of the receiver; thus, you may subtract by passing a negative delta. |
---|
154 | * |
---|
155 | * @param dHue The hue delta. |
---|
156 | * @param dSat The saturation delta. |
---|
157 | * @param dBrit The brightness delta. |
---|
158 | * @return The adjusted color, in the calibrated-RGB color space. |
---|
159 | */ |
---|
160 | - (NSColor *)adjustHue:(float)dHue saturation:(float)dSat brightness:(float)dBrit; |
---|
161 | |
---|
162 | @end |
---|
163 | |
---|
164 | @interface NSColor (AIColorAdditions_RepresentingColors) |
---|
165 | |
---|
166 | /*! @brief Returns the red, green, and blue components of the receiver as a hexadecimal string. |
---|
167 | * |
---|
168 | * @par Each component is represented by two hexadecimal digits; thus, the result will always be six characters long. |
---|
169 | * |
---|
170 | * @par Digits greater than 9 are lowercase. |
---|
171 | * |
---|
172 | * @par This method assumes that the receiver is in an RGB color space. If it isn't, you will get an exception. |
---|
173 | * |
---|
174 | * @return The hexadecimal string representing the receiver. |
---|
175 | */ |
---|
176 | - (NSString *)hexString; |
---|
177 | |
---|
178 | /*! @brief Returns the red, green, blue, and alpha components of the receiver as a triplet of decimal numbers from 0 to 255. |
---|
179 | * |
---|
180 | * @par The alpha component is only included in the result string if it is not 1. If the alpha component <em>is</em> 1, it will be omitted from the result. |
---|
181 | * |
---|
182 | * @par The three or four numbers are separated by commas, with no spaces. |
---|
183 | * |
---|
184 | * @par This method assumes that the receiver is in an RGB color space. If it isn't, you will get an exception. |
---|
185 | * |
---|
186 | * @par To convert the result from this method back to an \c NSColor, use <code>-[NSString(AIColorAdditions) representedColor]</code>. |
---|
187 | * |
---|
188 | * @return The decimal string representing the receiver. |
---|
189 | */ |
---|
190 | - (NSString *)stringRepresentation; |
---|
191 | |
---|
192 | /*! @brief Returns the red, green, blue, and alpha components of the receiver as a CSS color literal. |
---|
193 | * |
---|
194 | * @par If the receiver's alpha component is not 1, the literal will use CSS' <code>rgba()</code> function. If it is 1, the literal will be an HTML-style hex literal (e.g., <code>#ffffff</code>). |
---|
195 | * |
---|
196 | * @par The returned string is suitable for splicing into CSS code; for example, <code>[NSString stringWithFormat:@"color: %@;", [myColor CSSRepresentation]]</code>. |
---|
197 | * |
---|
198 | * @return A string of a CSS literal representing the receiver. |
---|
199 | */ |
---|
200 | - (NSString *)CSSRepresentation; |
---|
201 | |
---|
202 | @end |
---|
203 | |
---|
204 | /*! @brief Interprets a hexadecimal digit represented as a character. |
---|
205 | * |
---|
206 | * @par \a hex must be ASCII (including UTF-8 and all variants of ISO-8859). |
---|
207 | * |
---|
208 | * @par If \a is not a valid hexadecimal character, this function returns -1. Remember to check its return value. |
---|
209 | * |
---|
210 | * @return The number represented by the digit, or -1. |
---|
211 | */ |
---|
212 | int hexToInt(char hex); |
---|
213 | /*! @brief Converts a digit to a character representing a hexadecimal digit. |
---|
214 | * |
---|
215 | * @par \a digit must be in the range 0 to 15, inclusive. If it isn't, the return value is NUL (<code>'\0'</code>). |
---|
216 | * |
---|
217 | * @return A lowercase character, encoded in ASCII, representing the number in hexadecimal. |
---|
218 | */ |
---|
219 | char intToHex(int digit); |
---|
220 | |
---|
221 | @interface NSString (AIColorAdditions_RepresentingColors) |
---|
222 | |
---|
223 | /*! @brief Interprets a series of three or four decimal numbers as an RGB or RGBA color. |
---|
224 | * |
---|
225 | * @par The numbers must be separated by commas, and must range from 0 to 255, inclusive. Their order is red, green, blue, alpha. |
---|
226 | * |
---|
227 | * @par This method returns \c nil if the string does not contain exactly three or four numbers, or has anything before or after the numbers (except that whitespace before the first number is skipped). |
---|
228 | * |
---|
229 | * @par To create a string suitable for sending this message to, call <code>-[NSColor(AIColorAdditions) stringRepresentation]. |
---|
230 | * |
---|
231 | * @return A color in the calibrated RGB color space, or \c nil. |
---|
232 | */ |
---|
233 | - (NSColor *)representedColor; |
---|
234 | /*! @brief Interprets a series of three or four decimal numbers as an RGB color, and overrides the alpha component. |
---|
235 | * |
---|
236 | * @par The numbers must be separated by commas, and must range from 0 to 255, inclusive. Their order is red, green, blue, alpha. |
---|
237 | * |
---|
238 | * @par Unlike <code>-representedColor</code>, this method forces a specific alpha value in the returned color, ignoring (if it's present) the fourth value in the receiver. |
---|
239 | * |
---|
240 | * @par The numbers must be separated by commas, and must range from 0 to 255, inclusive. |
---|
241 | * |
---|
242 | * @par This method returns \c nil if the string does not contain exactly three or four numbers, or has anything before or after the numbers (except that whitespace before the first number is skipped). |
---|
243 | * |
---|
244 | * @par To create a string suitable for sending this message to, call <code>-[NSColor(AIColorAdditions) stringRepresentation]. |
---|
245 | * |
---|
246 | * @return A color in the calibrated-RGB color space, or \c nil. |
---|
247 | */ |
---|
248 | - (NSColor *)representedColorWithAlpha:(float)alpha; |
---|
249 | |
---|
250 | @end |
---|
251 | |
---|
252 | @interface NSColor (AIColorAdditions_RandomColor) |
---|
253 | |
---|
254 | /*! @brief Return a color with random red, green, and blue, with its alpha component set to 1. |
---|
255 | * |
---|
256 | * @par You do not need to seed a PRNG before calling this method. |
---|
257 | * |
---|
258 | * @par If you want the alpha to be random as well, use <code>+randomColorWithAlpha</code> instead. |
---|
259 | * |
---|
260 | * @return A random color in the calibrated-RGB color space. |
---|
261 | */ |
---|
262 | + (NSColor *)randomColor; |
---|
263 | /*! @brief Return a color with random red, green, blue, and alpha. |
---|
264 | * |
---|
265 | * @par You do not need to seed a PRNG before calling this method. |
---|
266 | * |
---|
267 | * @par If you want the alpha to always be 1 as well, use <code>+randomColor</code> instead. You can change the alpha to another static value using <code>-[NSColor colorWithAlphaComponent:]</code>. |
---|
268 | * |
---|
269 | * @return A random color in the calibrated-RGB color space. |
---|
270 | */ |
---|
271 | + (NSColor *)randomColorWithAlpha; |
---|
272 | |
---|
273 | @end |
---|
274 | |
---|
275 | @interface NSColor (AIColorAdditions_HTMLSVGCSSColors) |
---|
276 | |
---|
277 | /*! @brief Interpret an HTML color literal, with a specific fallback in case of a syntax error. |
---|
278 | * |
---|
279 | * @par As an extension, this method supports alpha. If \a str contains four or eight characters, the last quarter of the string is the alpha component. |
---|
280 | * |
---|
281 | * @par If \a str is shorter than six characters, this method will imagine zeros to fill the shortfall, as WebKit does. For example, â#ff000â (five characters) becomes â#ff0000â (six characters). This does not apply to the abbreviated syntax: a two-character string is illegal. |
---|
282 | * |
---|
283 | * @par If \a str is not a valid literal, this method returns \c nil. |
---|
284 | * |
---|
285 | * @par This method does not support CSS' <code>rgba()</code> function; therefore, we do not recommend using it as the inverse of <code>-CSSRepresentation</code>. |
---|
286 | * |
---|
287 | * @param str The string to interpret as an HTML color literal. |
---|
288 | * @return A color in the calibrated-RGB color space, or \c nil. |
---|
289 | */ |
---|
290 | + (id)colorWithHTMLString:(NSString *)str; |
---|
291 | /*! @brief Interpret an HTML color literal, with a specific fallback in case of a syntax error. |
---|
292 | * |
---|
293 | * @par As an extension, this method supports alpha. If \a str contains four or eight characters, the last quarter of the string is the alpha component. |
---|
294 | * |
---|
295 | * @par If \a str is shorter than six characters, this method will imagine zeros to fill the shortfall, as WebKit does. For example, â#ff000â (five characters) becomes â#ff0000â (six characters). This does not apply to the abbreviated syntax: a two-character string is illegal. |
---|
296 | * |
---|
297 | * @par If \a str is not a valid literal, this method returns \a defaultColor. You may pass \c nil for \a defaultColor. |
---|
298 | * |
---|
299 | * @par This method does not support CSS' <code>rgba()</code> function; therefore, we do not recommend using it as the inverse of <code>-CSSRepresentation</code>. |
---|
300 | * |
---|
301 | * @par If it creates the color from the input string, this method will use the calibrated-RGB color space. If, instead, it returns \a defaultColor, it will not convert it to the calibrated-RGB color space for you: you will get back whatever you passed in. |
---|
302 | * |
---|
303 | * @param str The string to interpret as an HTML color literal. |
---|
304 | * @param defaultColor The color to return if \a str is invalid. May be \c nil. |
---|
305 | * @return A color: either one created from \a str, or \a defaultColor. |
---|
306 | */ |
---|
307 | + (id)colorWithHTMLString:(NSString *)str defaultColor:(NSColor *)defaultColor; |
---|
308 | |
---|
309 | @end |
---|