Frameworks/AIUtilities Framework/Source/AIColorAdditions.m
author Evan Schoenberg
Thu, 14 Nov 2019 16:22:42 -0500
branchadium-1.5.10.5
changeset 5997 d88e6711d0e0
parent 3678 4dbda7e6999b
permissions -rw-r--r--
Fixed a triple handful of warnings
3678
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
     1
/* 
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
     2
 * Adium is the legal property of its developers, whose names are listed in the copyright file included
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
     3
 * with this source distribution.
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
     4
 * 
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
     6
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
     7
 * or (at your option) any later version.
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
     8
 * 
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
     9
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
    10
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
    11
 * Public License for more details.
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
    12
 * 
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
    13
 * You should have received a copy of the GNU General Public License along with this program; if not,
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
    14
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3073
diff changeset
    15
 */
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    16
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    17
/*
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    18
    Utilities for creating a NSColor from a hex string representation, and storing colors as a string
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    19
*/
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    20
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    21
#import "AIColorAdditions.h"
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    22
#import "AIStringAdditions.h"
2093
d470a05e220b I see no reason to not use #import for everything. Apple's compiler supports it fine in .c files, and it doesn't seem to have any downsides vs #include unless you're playing silly multiple inclusion tricks
David Smith <catfish.man@gmail.com>
parents: 1913
diff changeset
    23
#import <string.h>
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    24
2568
e0525a651346 Don't leak the color arrays for group chats.
Zachary West <zacw@adium.im>
parents: 2506
diff changeset
    25
static NSArray *defaultValidColors = nil;
2506
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
    26
#define VALID_COLORS_ARRAY [[NSArray alloc] initWithObjects:@"aqua", @"aquamarine", @"blue", @"blueviolet", @"brown", @"burlywood", @"cadetblue", @"chartreuse", @"chocolate", @"coral", @"cornflowerblue", @"crimson", @"cyan", @"darkblue", @"darkcyan", @"darkgoldenrod", @"darkgreen", @"darkgrey", @"darkkhaki", @"darkmagenta", @"darkolivegreen", @"darkorange", @"darkorchid", @"darkred", @"darksalmon", @"darkseagreen", @"darkslateblue", @"darkslategrey", @"darkturquoise", @"darkviolet", @"deeppink", @"deepskyblue", @"dimgrey", @"dodgerblue", @"firebrick", @"forestgreen", @"fuchsia", @"gold", @"goldenrod", @"green", @"greenyellow", @"grey", @"hotpink", @"indianred", @"indigo", @"lawngreen", @"lightblue", @"lightcoral", @"lightgreen", @"lightgrey", @"lightpink", @"lightsalmon", @"lightseagreen", @"lightskyblue", @"lightslategrey", @"lightsteelblue", @"lime", @"limegreen", @"magenta", @"maroon", @"mediumaquamarine", @"mediumblue", @"mediumorchid", @"mediumpurple", @"mediumseagreen", @"mediumslateblue", @"mediumspringgreen", @"mediumturquoise", @"mediumvioletred", @"midnightblue", @"navy", @"olive", @"olivedrab", @"orange", @"orangered", @"orchid", @"palegreen", @"paleturquoise", @"palevioletred", @"peru", @"pink", @"plum", @"powderblue", @"purple", @"red", @"rosybrown", @"royalblue", @"saddlebrown", @"salmon", @"sandybrown", @"seagreen", @"sienna", @"silver", @"skyblue", @"slateblue", @"slategrey", @"springgreen", @"steelblue", @"tan", @"teal", @"thistle", @"tomato", @"turquoise", @"violet", @"yellowgreen", nil]
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
    27
5997
d88e6711d0e0 Fixed a triple handful of warnings
Evan Schoenberg
parents: 3678
diff changeset
    28
/*
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
    29
static const CGFloat ONE_THIRD = 1.0f/3.0f;
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
    30
static const CGFloat ONE_SIXTH = 1.0f/6.0f;
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
    31
static const CGFloat TWO_THIRD = 2.0f/3.0f;
5997
d88e6711d0e0 Fixed a triple handful of warnings
Evan Schoenberg
parents: 3678
diff changeset
    32
*/
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    33
1913
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
    34
static NSMutableDictionary *RGBColorValues = nil;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    35
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    36
//two parts of a single path:
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    37
//	defaultRGBTxtLocation1/VERSION/defaultRGBTxtLocation2
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    38
static NSString *defaultRGBTxtLocation1 = @"/usr/share/emacs";
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    39
static NSString *defaultRGBTxtLocation2 = @"etc/rgb.txt";
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    40
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    41
#ifdef DEBUG_BUILD
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    42
	#define COLOR_DEBUG TRUE
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    43
#else
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    44
	#define COLOR_DEBUG FALSE
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    45
#endif
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    46
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    47
@implementation NSDictionary (AIColorAdditions_RGBTxtFiles)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    48
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    49
//see /usr/share/emacs/(some version)/etc/rgb.txt for an example of such a file.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    50
//the pathname does not need to end in 'rgb.txt', but it must be a file in UTF-8 encoding.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    51
//the keys are colour names (all converted to lowercase); the values are RGB NSColors.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    52
+ (id)dictionaryWithContentsOfRGBTxtFile:(NSString *)path
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    53
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    54
	NSMutableData *data = [NSMutableData dataWithContentsOfFile:path];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    55
	if (!data) return nil;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    56
	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    57
	char *ch = [data mutableBytes]; //we use mutable bytes because we want to tokenise the string by replacing separators with '\0'.
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
    58
	NSUInteger length = [data length];
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    59
	struct {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    60
		const char *redStart, *greenStart, *blueStart, *nameStart;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    61
		const char *redEnd,   *greenEnd,   *blueEnd;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    62
		float red, green, blue;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    63
		unsigned reserved: 23;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    64
		unsigned inComment: 1;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    65
		char prevChar;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    66
	} state = {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    67
		.prevChar = '\n',
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    68
		.redStart = NULL, .greenStart = NULL, .blueStart = NULL, .nameStart = NULL,
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    69
		.inComment = NO,
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    70
	};
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    71
	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    72
	NSDictionary *result = nil;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    73
	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    74
	//the rgb.txt file that comes with Mac OS X 10.3.8 contains 752 entries.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    75
	//we create 3 autoreleased objects for each one.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    76
	//best to not pollute our caller's autorelease pool.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    77
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    78
	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    79
	NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    80
	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    81
	for (unsigned i = 0; i < length; ++i) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    82
		if (state.inComment) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    83
			if (ch[i] == '\n') state.inComment = NO;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    84
		} else if (ch[i] == '\n') {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    85
			if (state.prevChar != '\n') { //ignore blank lines
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    86
				if (	! ((state.redStart   != NULL)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    87
					   && (state.greenStart != NULL)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    88
					   && (state.blueStart  != NULL)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    89
					   && (state.nameStart  != NULL)))
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    90
				{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    91
#if COLOR_DEBUG
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    92
					NSLog(@"Parse error reading rgb.txt file: a non-comment line was encountered that did not have all four of red (%p), green (%p), blue (%p), and name (%p) - index is %u",
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    93
						  state.redStart,
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    94
						  state.greenStart,
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    95
						  state.blueStart,
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    96
						  state.nameStart, i);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    97
#endif
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    98
					goto end;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
    99
				}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   100
				
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   101
				NSRange range = {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   102
					.location = state.nameStart - ch,
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   103
					.length   = (&ch[i]) - state.nameStart,
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   104
				};
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   105
				NSString *name = [NSString stringWithData:[data subdataWithRange:range] encoding:NSUTF8StringEncoding];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   106
				NSColor *color = [NSColor colorWithCalibratedRed:state.red
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   107
														   green:state.green
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   108
															blue:state.blue
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   109
														   alpha:1.0f];
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   110
				[mutableDict setObject:color forKey:name];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   111
				NSString *lowercaseName = [name lowercaseString];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   112
				if (![mutableDict objectForKey:lowercaseName]) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   113
					//only add the lowercase version if it isn't already defined
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   114
					[mutableDict setObject:color forKey:lowercaseName];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   115
				}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   116
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   117
				state.redStart = state.greenStart = state.blueStart = state.nameStart = 
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   118
				state.redEnd   = state.greenEnd   = state.blueEnd   = NULL;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   119
			} //if (prevChar != '\n')
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   120
		} else if ((ch[i] != ' ') && (ch[i] != '\t')) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   121
			if (state.prevChar == '\n' && ch[i] == '#') {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   122
				state.inComment = YES;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   123
			} else {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   124
				if (!state.redStart) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   125
					state.redStart = &ch[i];
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   126
					state.red = (float)(strtod(state.redStart, (char **)&state.redEnd) / 255.0f);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   127
				} else if ((!state.greenStart) && state.redEnd && (&ch[i] >= state.redEnd)) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   128
					state.greenStart = &ch[i];
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   129
					state.green = (float)(strtod(state.greenStart, (char **)&state.greenEnd) / 255.0f);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   130
				} else if ((!state.blueStart) && state.greenEnd && (&ch[i] >= state.greenEnd)) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   131
					state.blueStart = &ch[i];
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   132
					state.blue = (float)(strtod(state.blueStart, (char **)&state.blueEnd) / 255.0f);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   133
				} else if ((!state.nameStart) && state.blueEnd && (&ch[i] >= state.blueEnd)) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   134
					state.nameStart  = &ch[i];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   135
				}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   136
			}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   137
		}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   138
		state.prevChar = ch[i];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   139
	} //for (unsigned i = 0; i < length; ++i)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   140
	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   141
	//why not use -copy? because this is subclass-friendly.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   142
	//you can call this method on NSMutableDictionary and get a mutable dictionary back.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   143
	result = [[self alloc] initWithDictionary:mutableDict];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   144
end:
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   145
	[pool release];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   146
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   147
	return [result autorelease];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   148
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   149
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   150
@end
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   151
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   152
@implementation NSColor (AIColorAdditions_RGBTxtFiles)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   153
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   154
+ (NSDictionary *)colorNamesDictionary
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   155
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   156
	if (!RGBColorValues) {
1913
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   157
		RGBColorValues = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   158
						  [NSColor colorWithHTMLString:@"#000"],    @"black",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   159
						  [NSColor colorWithHTMLString:@"#c0c0c0"], @"silver",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   160
						  [NSColor colorWithHTMLString:@"#808080"], @"gray",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   161
						  [NSColor colorWithHTMLString:@"#808080"], @"grey",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   162
						  [NSColor colorWithHTMLString:@"#fff"],    @"white",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   163
						  [NSColor colorWithHTMLString:@"#800000"], @"maroon",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   164
						  [NSColor colorWithHTMLString:@"#f00"],    @"red",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   165
						  [NSColor colorWithHTMLString:@"#800080"], @"purple",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   166
						  [NSColor colorWithHTMLString:@"#f0f"],    @"fuchsia",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   167
						  [NSColor colorWithHTMLString:@"#008000"], @"green",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   168
						  [NSColor colorWithHTMLString:@"#0f0"],    @"lime",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   169
						  [NSColor colorWithHTMLString:@"#808000"], @"olive",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   170
						  [NSColor colorWithHTMLString:@"#ff0"],    @"yellow",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   171
						  [NSColor colorWithHTMLString:@"#000080"], @"navy",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   172
						  [NSColor colorWithHTMLString:@"#00f"],    @"blue",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   173
						  [NSColor colorWithHTMLString:@"#008080"], @"teal",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   174
						  [NSColor colorWithHTMLString:@"#0ff"],    @"aqua",
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   175
						  nil];
881
6d504e30cc4f Future warnings -= 3;
David Smith
parents: 0
diff changeset
   176
		NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:defaultRGBTxtLocation1 error:NULL];
6d504e30cc4f Future warnings -= 3;
David Smith
parents: 0
diff changeset
   177
		for (NSString *middlePath in paths) {
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   178
			NSString *path = [defaultRGBTxtLocation1 stringByAppendingPathComponent:[middlePath stringByAppendingPathComponent:defaultRGBTxtLocation2]];
1913
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   179
			NSDictionary *extraColors = [NSDictionary dictionaryWithContentsOfRGBTxtFile:path];
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   180
			[RGBColorValues addEntriesFromDictionary:extraColors];
51874aa4b6db Make sure the base set of html colors is always defined, then add any extras we have to that. Avoids some annoying console logging about not knowing what 'teal' is
David Smith <catfish.man@gmail.com>
parents: 881
diff changeset
   181
			if (extraColors) {
881
6d504e30cc4f Future warnings -= 3;
David Smith
parents: 0
diff changeset
   182
#if COLOR_DEBUG
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   183
				NSLog(@"Got colour values from %@", path);
881
6d504e30cc4f Future warnings -= 3;
David Smith
parents: 0
diff changeset
   184
#endif
6d504e30cc4f Future warnings -= 3;
David Smith
parents: 0
diff changeset
   185
				break;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   186
			}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   187
		}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   188
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   189
	return RGBColorValues;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   190
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   191
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   192
@end
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   193
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   194
@implementation NSColor (AIColorAdditions_Comparison)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   195
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   196
//Returns YES if the colors are equal
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   197
- (BOOL)equalToRGBColor:(NSColor *)inColor
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   198
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   199
    NSColor	*convertedA = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   200
    NSColor	*convertedB = [inColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   201
    
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   202
    return (([convertedA redComponent]   == [convertedB redComponent])   &&
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   203
            ([convertedA blueComponent]  == [convertedB blueComponent])  &&
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   204
            ([convertedA greenComponent] == [convertedB greenComponent]) &&
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   205
            ([convertedA alphaComponent] == [convertedB alphaComponent]));
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   206
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   207
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   208
@end
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   209
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   210
@implementation NSColor (AIColorAdditions_DarknessAndContrast)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   211
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   212
//Returns YES if this color is dark
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   213
- (BOOL)colorIsDark
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   214
{
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   215
    return ([[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace] brightnessComponent] < 0.5f);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   216
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   217
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   218
- (BOOL)colorIsMedium
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   219
{
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   220
	CGFloat brightness = [[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace] brightnessComponent];
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   221
	return (0.35f < brightness && brightness < 0.65f);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   222
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   223
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   224
//Percent should be -1.0 to 1.0 (negatives will make the color brighter)
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   225
- (NSColor *)darkenBy:(CGFloat)amount
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   226
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   227
    NSColor	*convertedColor = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   228
    
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   229
    return [NSColor colorWithCalibratedHue:[convertedColor hueComponent]
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   230
                                saturation:[convertedColor saturationComponent]
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   231
                                brightness:([convertedColor brightnessComponent] - amount)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   232
                                     alpha:[convertedColor alphaComponent]];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   233
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   234
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   235
- (NSColor *)darkenAndAdjustSaturationBy:(CGFloat)amount
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   236
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   237
    NSColor	*convertedColor = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   238
    
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   239
    return [NSColor colorWithCalibratedHue:[convertedColor hueComponent]
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   240
                                saturation:(([convertedColor saturationComponent] == 0.0f) ? [convertedColor saturationComponent] : ([convertedColor saturationComponent] + amount))
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   241
                                brightness:([convertedColor brightnessComponent] - amount)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   242
                                     alpha:[convertedColor alphaComponent]];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   243
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   244
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   245
//Inverts the luminance of this color so it looks good on selected/dark backgrounds
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   246
- (NSColor *)colorWithInvertedLuminance
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   247
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   248
    CGFloat h,l,s;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   249
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   250
	NSColor *convertedColor = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   251
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   252
    //Get our HLS
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   253
    [convertedColor getHue:&h saturation:&s brightness:&l alpha:NULL];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   254
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   255
    //Invert L
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   256
    l = 1.0f - l;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   257
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   258
    //Return the new color
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   259
    return [NSColor colorWithCalibratedHue:h saturation:s brightness:l alpha:1.0f];
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   260
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   261
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   262
//Returns a color that contrasts well with this one
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   263
- (NSColor *)contrastingColor
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   264
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   265
	if ([self colorIsMedium]) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   266
		if ([self colorIsDark])
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   267
			return [NSColor whiteColor];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   268
		else
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   269
			return [NSColor blackColor];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   270
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   271
	} else {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   272
		NSColor *rgbColor = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   273
		return [NSColor colorWithCalibratedRed:(1.0f - [rgbColor redComponent])
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   274
										 green:(1.0f - [rgbColor greenComponent])
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   275
										  blue:(1.0f - [rgbColor blueComponent])
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   276
										 alpha:1.0f];
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   277
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   278
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   279
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   280
@end
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   281
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   282
@implementation NSColor (AIColorAdditions_HLS)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   283
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   284
//Linearly adjust a color
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   285
#define cap(x) { if (x < 0) {x = 0;} else if (x > 1) {x = 1;} }
2677
43d104027d33 Correct for 64-bit type mismatches for our unit tests.
Stephen Holt <sholt@adium.im>
parents: 2568
diff changeset
   286
- (NSColor *)adjustHue:(CGFloat)dHue saturation:(CGFloat)dSat brightness:(CGFloat)dBrit
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   287
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   288
    CGFloat hue, sat, brit, alpha;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   289
    
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   290
    [self getHue:&hue saturation:&sat brightness:&brit alpha:&alpha];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   291
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   292
	//For some reason, redColor's hue is 1.0f, not 0.0f, as of Mac OS X 10.4.10 and 10.5.2. Therefore, we must normalize any multiple of 1.0 to 0.0. We do this by taking the remainder of hue ?? 1.
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   293
	hue = AIfmod(hue, 1.0f);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   294
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   295
    hue += dHue;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   296
    cap(hue);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   297
    sat += dSat;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   298
    cap(sat);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   299
    brit += dBrit;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   300
    cap(brit);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   301
    
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   302
    return [NSColor colorWithCalibratedHue:hue saturation:sat brightness:brit alpha:alpha];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   303
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   304
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   305
@end
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   306
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   307
@implementation NSColor (AIColorAdditions_RepresentingColors)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   308
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   309
- (NSString *)hexString
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   310
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   311
    CGFloat 	red,green,blue;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   312
    char	hexString[7];
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   313
    NSInteger		tempNum;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   314
    NSColor	*convertedColor;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   315
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   316
    convertedColor = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   317
    [convertedColor getRed:&red green:&green blue:&blue alpha:NULL];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   318
    
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   319
    tempNum = (red * 255.0f);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   320
    hexString[0] = intToHex(tempNum / 16);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   321
    hexString[1] = intToHex(tempNum % 16);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   322
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   323
    tempNum = (green * 255.0f);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   324
    hexString[2] = intToHex(tempNum / 16);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   325
    hexString[3] = intToHex(tempNum % 16);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   326
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   327
    tempNum = (blue * 255.0f);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   328
    hexString[4] = intToHex(tempNum / 16);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   329
    hexString[5] = intToHex(tempNum % 16);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   330
    hexString[6] = '\0';
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   331
    
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   332
    return [NSString stringWithUTF8String:hexString];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   333
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   334
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   335
//String representation: R,G,B[,A].
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   336
- (NSString *)stringRepresentation
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   337
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   338
    NSColor	*tempColor = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   339
	CGFloat alphaComponent = [tempColor alphaComponent];
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   340
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   341
	if (alphaComponent == 1.0) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   342
		return [NSString stringWithFormat:@"%d,%d,%d",
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   343
			(int)([tempColor redComponent] * 255.0),
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   344
			(int)([tempColor greenComponent] * 255.0),
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   345
			(int)([tempColor blueComponent] * 255.0)];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   346
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   347
	} else {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   348
		return [NSString stringWithFormat:@"%d,%d,%d,%d",
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   349
			(int)([tempColor redComponent] * 255.0),
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   350
			(int)([tempColor greenComponent] * 255.0),
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   351
			(int)([tempColor blueComponent] * 255.0),
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   352
			(int)(alphaComponent * 255.0)];		
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   353
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   354
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   355
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   356
- (NSString *)CSSRepresentation
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   357
{
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   358
	CGFloat alpha = [self alphaComponent];
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   359
	if ((1.0 - alpha) >= 0.000001) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   360
		NSColor *rgb = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   361
		//CSS3 defines rgba() to take 0..255 for the color components, but 0..1 for the alpha component. Thus, we must multiply by 255 for the color components, but not for the alpha component.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   362
		return [NSString stringWithFormat:@"rgba(%@,%@,%@,%@)",
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   363
			[NSString stringWithCGFloat:[rgb redComponent]   * 255.0f maxDigits:6],
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   364
			[NSString stringWithCGFloat:[rgb greenComponent] * 255.0f maxDigits:6],
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   365
			[NSString stringWithCGFloat:[rgb blueComponent]  * 255.0f maxDigits:6],
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   366
			[NSString stringWithCGFloat:alpha                         maxDigits:6]];
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   367
	} else {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   368
		return [@"#" stringByAppendingString:[self hexString]];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   369
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   370
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   371
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   372
@end
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   373
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   374
@implementation NSString (AIColorAdditions_RepresentingColors)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   375
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   376
- (NSColor *)representedColor
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   377
{
3073
601a5c302322 This is a simpler way..
Stephen Holt <sholt@adium.im>
parents: 3072
diff changeset
   378
    CGFloat	r = 255, g = 255, b = 255;
601a5c302322 This is a simpler way..
Stephen Holt <sholt@adium.im>
parents: 3072
diff changeset
   379
    CGFloat	a = 255;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   380
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   381
	const char *selfUTF8 = [self UTF8String];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   382
	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   383
	//format: r,g,b[,a]
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   384
	//all components are decimal numbers 0..255.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   385
	if (!isdigit(*selfUTF8)) goto scanFailed;
3073
601a5c302322 This is a simpler way..
Stephen Holt <sholt@adium.im>
parents: 3072
diff changeset
   386
	r = (CGFloat)strtoul(selfUTF8, (char **)&selfUTF8, /*base*/ 10);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   387
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   388
	if(*selfUTF8 == ',') ++selfUTF8;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   389
	else                 goto scanFailed;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   390
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   391
	if (!isdigit(*selfUTF8)) goto scanFailed;
3073
601a5c302322 This is a simpler way..
Stephen Holt <sholt@adium.im>
parents: 3072
diff changeset
   392
	g = (CGFloat)strtoul(selfUTF8, (char **)&selfUTF8, /*base*/ 10);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   393
	if(*selfUTF8 == ',') ++selfUTF8;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   394
	else                 goto scanFailed;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   395
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   396
	if (!isdigit(*selfUTF8)) goto scanFailed;
3073
601a5c302322 This is a simpler way..
Stephen Holt <sholt@adium.im>
parents: 3072
diff changeset
   397
	b = (CGFloat)strtoul(selfUTF8, (char **)&selfUTF8, /*base*/ 10);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   398
	if (*selfUTF8 == ',') {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   399
		++selfUTF8;
3073
601a5c302322 This is a simpler way..
Stephen Holt <sholt@adium.im>
parents: 3072
diff changeset
   400
		a = (CGFloat)strtoul(selfUTF8, (char **)&selfUTF8, /*base*/ 10);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   401
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   402
		if (*selfUTF8) goto scanFailed;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   403
	} else if (*selfUTF8 != '\0') {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   404
		goto scanFailed;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   405
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   406
3073
601a5c302322 This is a simpler way..
Stephen Holt <sholt@adium.im>
parents: 3072
diff changeset
   407
    return [NSColor colorWithCalibratedRed:(r/255) green:(g/255) blue:(b/255) alpha:(a/255)] ;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   408
scanFailed:
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   409
	return nil;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   410
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   411
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   412
- (NSColor *)representedColorWithAlpha:(CGFloat)alpha
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   413
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   414
	//this is the same as above, but the alpha component is overridden.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   415
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   416
  NSUInteger	r, g, b;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   417
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   418
	const char *selfUTF8 = [self UTF8String];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   419
	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   420
	//format: r,g,b
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   421
	//all components are decimal numbers 0..255.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   422
	if (!isdigit(*selfUTF8)) goto scanFailed;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   423
	r = strtoul(selfUTF8, (char **)&selfUTF8, /*base*/ 10);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   424
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   425
	if (*selfUTF8 != ',') goto scanFailed;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   426
	++selfUTF8;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   427
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   428
	if (!isdigit(*selfUTF8)) goto scanFailed;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   429
	g = strtoul(selfUTF8, (char **)&selfUTF8, /*base*/ 10);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   430
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   431
	if (*selfUTF8 != ',') goto scanFailed;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   432
	++selfUTF8;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   433
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   434
	if (!isdigit(*selfUTF8)) goto scanFailed;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   435
	b = strtoul(selfUTF8, (char **)&selfUTF8, /*base*/ 10);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   436
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   437
    return [NSColor colorWithCalibratedRed:(r/255) green:(g/255) blue:(b/255) alpha:alpha];
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   438
scanFailed:
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   439
	return nil;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   440
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   441
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   442
@end
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   443
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   444
@implementation NSColor (AIColorAdditions_RandomColor)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   445
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   446
+ (NSColor *)randomColor
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   447
{
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   448
	return [NSColor colorWithCalibratedRed:(arc4random() % 65536) / 65536.0f
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   449
	                                 green:(arc4random() % 65536) / 65536.0f
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   450
	                                  blue:(arc4random() % 65536) / 65536.0f
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   451
	                                 alpha:1.0f];
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   452
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   453
+ (NSColor *)randomColorWithAlpha
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   454
{
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   455
	return [NSColor colorWithCalibratedRed:(arc4random() % 65536) / 65536.0f
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   456
	                                 green:(arc4random() % 65536) / 65536.0f
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   457
	                                  blue:(arc4random() % 65536) / 65536.0f
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   458
	                                 alpha:(arc4random() % 65536) / 65536.0f];
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   459
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   460
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   461
@end
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   462
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   463
@implementation NSColor (AIColorAdditions_HTMLSVGCSSColors)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   464
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   465
+ (id)colorWithHTMLString:(NSString *)str
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   466
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   467
	return [self colorWithHTMLString:str defaultColor:nil];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   468
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   469
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   470
/*!
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   471
 * @brief Convert one or two hex characters to a float
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   472
 *
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   473
 * @param firstChar The first hex character
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   474
 * @param secondChar The second hex character, or 0x0 if only one character is to be used
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   475
 * @result The float value. Returns 0 as a bailout value if firstChar or secondChar are not valid hexadecimal characters ([0-9]|[A-F]|[a-f]). Also returns 0 if firstChar and secondChar equal 0.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   476
 */
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   477
static CGFloat hexCharsToFloat(char firstChar, char secondChar)
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   478
{
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   479
	CGFloat				hexValue;
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   480
	NSUInteger		firstDigit;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   481
	firstDigit = hexToInt(firstChar);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   482
	if (firstDigit != -1) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   483
		hexValue = firstDigit;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   484
		if (secondChar != 0x0) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   485
			int secondDigit = hexToInt(secondChar);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   486
			if (secondDigit != -1)
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   487
				hexValue = (hexValue * 16.0f + secondDigit) / 255.0f;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   488
			else
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   489
				hexValue = 0;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   490
		} else {
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   491
			hexValue /= 15.0f;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   492
		}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   493
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   494
	} else {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   495
		hexValue = 0;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   496
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   497
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   498
	return hexValue;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   499
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   500
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   501
+ (id)colorWithHTMLString:(NSString *)str defaultColor:(NSColor *)defaultColor
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   502
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   503
	if (!str) return defaultColor;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   504
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   505
	NSUInteger strLength = [str length];
2230
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   506
	
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   507
	NSString *colorValue = str;
2230
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   508
	
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   509
	if ([str hasPrefix:@"rgb"]) {
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   510
		NSUInteger leftParIndex = [colorValue rangeOfString:@"("].location;
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   511
		NSUInteger rightParIndex = [colorValue rangeOfString:@")"].location;
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   512
		if (leftParIndex == NSNotFound || rightParIndex == NSNotFound)
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   513
		{
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   514
			NSLog(@"+[NSColor(AIColorAdditions) colorWithHTMLString:] called with unrecognised color function (str is %@); returning %@", str, defaultColor);
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   515
			return defaultColor;
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   516
		}
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   517
		leftParIndex++;
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   518
		NSRange substrRange = NSMakeRange(leftParIndex, rightParIndex - leftParIndex);
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   519
		colorValue = [colorValue substringWithRange:substrRange];
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   520
		NSArray *colorComponents = [colorValue componentsSeparatedByString:@","];
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   521
		if ([colorComponents count] < 3 || [colorComponents count] > 4) {
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   522
			NSLog(@"+[NSColor(AIColorAdditions) colorWithHTMLString:] called with a color function with the wrong number of arguments (str is %@); returning %@", str, defaultColor);
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   523
			return defaultColor;
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   524
		}
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   525
		float red, green, blue, alpha = 1.0f;
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   526
		red = [[colorComponents objectAtIndex:0] floatValue];
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   527
		green = [[colorComponents objectAtIndex:1] floatValue];
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   528
		blue = [[colorComponents objectAtIndex:2] floatValue];
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   529
		if ([colorComponents count] == 4)
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   530
			alpha = [[colorComponents objectAtIndex:3] floatValue];
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   531
		return [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   532
	}
fb9497a35bb0 rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me
David Smith <catfish.man@gmail.com>
parents: 2093
diff changeset
   533
	
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   534
	if ((!strLength) || ([str characterAtIndex:0] != '#')) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   535
		//look it up; it's a colour name
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   536
		NSDictionary *colorValues = [self colorNamesDictionary];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   537
		colorValue = [colorValues objectForKey:str];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   538
		if (!colorValue) colorValue = [colorValues objectForKey:[str lowercaseString]];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   539
		if (!colorValue) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   540
#if COLOR_DEBUG
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   541
			NSLog(@"+[NSColor(AIColorAdditions) colorWithHTMLString:] called with unrecognised color name (str is %@); returning %@", str, defaultColor);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   542
#endif
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   543
			return defaultColor;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   544
		}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   545
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   546
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   547
	//we need room for at least 9 characters (#00ff00ff) plus the NUL terminator.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   548
	//this array is 12 bytes long because I like multiples of four. ;)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   549
	enum { hexStringArrayLength = 12 };
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   550
	size_t hexStringLength = 0;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   551
	char hexStringArray[hexStringArrayLength] = { 0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0, };
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   552
	{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   553
		NSData *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   554
		hexStringLength = [stringData length];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   555
		//subtract 1 because we don't want to overwrite that last NUL.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   556
		memcpy(hexStringArray, [stringData bytes], MIN(hexStringLength, hexStringArrayLength - 1));
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   557
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   558
	const char *hexString = hexStringArray;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   559
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   560
	CGFloat		red,green,blue;
3071
b21c2cff5c86 Fix datatypes for safety on both 32 and 64 bit arches.
Stephen Holt <sholt@adium.im>
parents: 3062
diff changeset
   561
	CGFloat		alpha = 1.0f;
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   562
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   563
	//skip # if present.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   564
	if (*hexString == '#') {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   565
		++hexString;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   566
		--hexStringLength;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   567
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   568
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   569
	if (hexStringLength < 3) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   570
#if COLOR_DEBUG
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   571
		NSLog(@"+[%@ colorWithHTMLString:] called with a string that cannot possibly be a hexadecimal color specification (e.g. #ff0000, #00b, #cc08) (string: %@ input: %@); returning %@", NSStringFromClass(self), colorValue, str, defaultColor);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   572
#endif
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   573
		return defaultColor;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   574
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   575
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   576
	//long specification:  #rrggbb[aa]
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   577
	//short specification: #rgb[a]
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   578
	//e.g. these all specify pure opaque blue: #0000ff #00f #0000ffff #00ff
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   579
	BOOL isLong = hexStringLength > 4;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   580
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   581
	//for a long component c = 'xy':
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   582
	//	c = (x * 0x10 + y) / 0xff
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   583
	//for a short component c = 'x':
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   584
	//	c = x / 0xf
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   585
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   586
	char firstChar, secondChar;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   587
	
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   588
	firstChar = *(hexString++);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   589
	secondChar = (isLong ? *(hexString++) : 0x0);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   590
	red = hexCharsToFloat(firstChar, secondChar);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   591
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   592
	firstChar = *(hexString++);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   593
	secondChar = (isLong ? *(hexString++) : 0x0);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   594
	green = hexCharsToFloat(firstChar, secondChar);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   595
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   596
	firstChar = *(hexString++);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   597
	secondChar = (isLong ? *(hexString++) : 0x0);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   598
	blue = hexCharsToFloat(firstChar, secondChar);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   599
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   600
	if (*hexString) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   601
		//we still have one more component to go: this is alpha.
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   602
		//without this component, alpha defaults to 1.0 (see initialiser above).
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   603
		firstChar = *(hexString++);
2999
04e978ab6aaf Fixing another dead store flagged by the analyzer.
Peter Hosey <hg@boredzo.org>
parents: 2677
diff changeset
   604
		secondChar = (isLong ? *hexString : 0x0);
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   605
		alpha = hexCharsToFloat(firstChar, secondChar);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   606
	}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   607
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   608
	return [self colorWithCalibratedRed:red green:green blue:blue alpha:alpha];
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   609
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   610
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   611
@end
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   612
2506
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   613
@implementation NSColor (AIColorAdditions_ObjectColor)
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   614
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   615
+ (NSString *)representedColorForObject: (id)anObject withValidColors: (NSArray *)validColors
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   616
{
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   617
	NSArray *validColorsArray = validColors;
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   618
2568
e0525a651346 Don't leak the color arrays for group chats.
Zachary West <zacw@adium.im>
parents: 2506
diff changeset
   619
	if (!validColorsArray || [validColorsArray count] == 0) {
e0525a651346 Don't leak the color arrays for group chats.
Zachary West <zacw@adium.im>
parents: 2506
diff changeset
   620
		if (!defaultValidColors) {
e0525a651346 Don't leak the color arrays for group chats.
Zachary West <zacw@adium.im>
parents: 2506
diff changeset
   621
			defaultValidColors = VALID_COLORS_ARRAY;
e0525a651346 Don't leak the color arrays for group chats.
Zachary West <zacw@adium.im>
parents: 2506
diff changeset
   622
		}
e0525a651346 Don't leak the color arrays for group chats.
Zachary West <zacw@adium.im>
parents: 2506
diff changeset
   623
		validColorsArray = defaultValidColors;
e0525a651346 Don't leak the color arrays for group chats.
Zachary West <zacw@adium.im>
parents: 2506
diff changeset
   624
	}
2506
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   625
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   626
	return [validColorsArray objectAtIndex:([anObject hash] % ([validColorsArray count]))];
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   627
}
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   628
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   629
@end
ef0c39fdfdaf Patch from Ariel Chinn to make user coloring generic so it can be used in the log viewer
David Smith <catfish.man@gmail.com>
parents: 2230
diff changeset
   630
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   631
//Convert hex to an int
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   632
int hexToInt(char hex)
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   633
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   634
    if (hex >= '0' && hex <= '9') {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   635
        return (hex - '0');
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   636
    } else if (hex >= 'a' && hex <= 'f') {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   637
        return (hex - 'a' + 10);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   638
    } else if (hex >= 'A' && hex <= 'F') {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   639
        return (hex - 'A' + 10);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   640
    } else {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   641
        return -1;
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   642
    }
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   643
}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   644
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   645
//Convert int to a hex
3062
e04a00f58ce6 Fixing implicit 64->32 bit casts in AIUtilities.
Stephen Holt <sholt@adium.im>
parents: 2999
diff changeset
   646
char intToHex(NSInteger digit)
0
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   647
{
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   648
    if (digit > 9) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   649
		if (digit <= 0xf) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   650
			return ('a' + digit - 10);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   651
		}
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   652
    } else if (digit >= 0) {
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   653
        return ('0' + digit);
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   654
    }
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   655
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   656
	return '\0'; //NUL
e22ad6bc8b46 svn 1.5 merge tracking is terrible. This is a manual merge of the Sparkle 1.5 branch into trunk
David Smith
parents:
diff changeset
   657
}