Commit 3625e968 authored by Adrian Robert's avatar Adrian Robert
Browse files

* nsterm.m (ns_get_color): Update documentation properly for last

	change, and clean up loose ends in the code left by it.  Fix
	longstanding bug with 16-bit hex parsing, and add support for
	yet another X11 format (rgb:r/g/b) for compatibility.
parent 641d87f5
...@@ -1346,13 +1346,14 @@ Free a pool and temporary objects it refers to (callable from C) ...@@ -1346,13 +1346,14 @@ Free a pool and temporary objects it refers to (callable from C)
/* -------------------------------------------------------------------------- /* --------------------------------------------------------------------------
Parse a color name Parse a color name
/* -------------------------------------------------------------------------- /* --------------------------------------------------------------------------
/* On *Step, we recognize several color formats, in addition to a catalog /* On *Step, we attempt to mimic the X11 platform here, down to installing an
of colors found in the file Emacs.clr. Color formats include: X11 rgb.txt-compatible color list in Emacs.clr (see ns_term_init()).
- #rrggbb where rr, gg, bb specify red, green and blue in hex. */ See: http://thread.gmane.org/gmane.emacs.devel/113050/focus=113272). */
{ {
NSColor * new = nil; NSColor *new = nil;
const char *hex = NULL; static char hex[20];
enum { rgb } color_space; int scaling;
float r = -1.0, g, b;
NSString *nsname = [NSString stringWithUTF8String: name]; NSString *nsname = [NSString stringWithUTF8String: name];
/*fprintf (stderr, "ns_get_color: '%s'\n", name); */ /*fprintf (stderr, "ns_get_color: '%s'\n", name); */
...@@ -1364,60 +1365,53 @@ Free a pool and temporary objects it refers to (callable from C) ...@@ -1364,60 +1365,53 @@ Free a pool and temporary objects it refers to (callable from C)
name = [ns_selection_color UTF8String]; name = [ns_selection_color UTF8String];
} }
if (name[0] == '0' || name[0] == '1' || name[0] == '.') /* First, check for some sort of numeric specification. */
hex[0] = '\0';
if (name[0] == '0' || name[0] == '1' || name[0] == '.') /* RGB decimal */
{ {
/* RGB decimal */
NSScanner *scanner = [NSScanner scannerWithString: nsname]; NSScanner *scanner = [NSScanner scannerWithString: nsname];
float r, g, b;
[scanner scanFloat: &r]; [scanner scanFloat: &r];
[scanner scanFloat: &g]; [scanner scanFloat: &g];
[scanner scanFloat: &b]; [scanner scanFloat: &b];
*col = [NSColor colorWithCalibratedRed: r green: g blue: b alpha: 1.0];
UNBLOCK_INPUT;
return 0;
} }
else if (!strncmp(name, "rgb:", 4)) /* A newer X11 format -- rgb:r/g/b */
if (name[0] == '#') /* X11 format */
{ {
hex = name + 1; strcpy(hex, name + 4);
color_space = rgb; scaling = (strlen(hex) - 2) / 4;
}
else if (name[0] == '#') /* An old X11 format; convert to newer */
{
int len = (strlen(name) - 1);
int start = (len % 3 == 0) ? 1 : len / 4 + 1;
int i;
scaling = strlen(name+start) / 3;
for (i=0; i<3; i++) {
strncpy(hex + i * (scaling + 1), name + start + i * scaling, scaling);
hex[(i+1) * (scaling + 1) - 1] = '/';
}
hex[3 * (scaling + 1) - 1] = '\0';
} }
/* Direct colors (hex values) */ if (hex[0])
if (hex)
{ {
unsigned long long color = 0; int rr, gg, bb;
if (sscanf (hex, "%x", &color)) float fscale = scaling == 4 ? 65535.0 : (scaling == 2 ? 255.0 : 15.0);
if (sscanf (hex, "%x/%x/%x", &rr, &gg, &bb))
{ {
float f1, f2, f3, f4; r = rr / fscale;
/* Assume it's either 1 byte or 2 per channel... */ g = gg / fscale;
if (strlen(hex) > 8) { b = bb / fscale;
f1 = ((color >> 48) & 0xffff) / 65535.0;
f2 = ((color >> 32) & 0xffff) / 65535.0;
f3 = ((color >> 16) & 0xffff) / 65535.0;
f4 = ((color ) & 0xffff) / 65535.0;
} else {
f1 = ((color >> 24) & 0xff) / 255.0;
f2 = ((color >> 16) & 0xff) / 255.0;
f3 = ((color >> 8) & 0xff) / 255.0;
f4 = ((color ) & 0xff) / 255.0;
}
switch (color_space)
{
case rgb:
*col = [NSColor colorWithCalibratedRed: f2
green: f3
blue: f4
alpha: 1.0];
break;
}
*col = [*col colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
UNBLOCK_INPUT;
return 0;
} }
} }
if (r >= 0.0)
{
*col = [NSColor colorWithCalibratedRed: r green: g blue: b alpha: 1.0];
UNBLOCK_INPUT;
return 0;
}
/* Otherwise, color is expected to be from a list */ /* Otherwise, color is expected to be from a list */
{ {
NSEnumerator *lenum, *cenum; NSEnumerator *lenum, *cenum;
...@@ -1444,10 +1438,8 @@ Free a pool and temporary objects it refers to (callable from C) ...@@ -1444,10 +1438,8 @@ Free a pool and temporary objects it refers to (callable from C)
} }
} }
if ( new ) if (new)
*col = [new colorUsingColorSpaceName: NSCalibratedRGBColorSpace]; *col = [new colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
/* else
NSLog (@"Failed to find color '%@'", nsname); */
UNBLOCK_INPUT; UNBLOCK_INPUT;
return new ? 0 : 1; return new ? 0 : 1;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment