aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAdrian Robert2009-08-21 19:31:48 +0000
committerAdrian Robert2009-08-21 19:31:48 +0000
commit3625e9685c707902ce1ecce411cb97afc75f0405 (patch)
tree7b0937e6622a0bcc8e7a44e6b10ef5d633b6df9e /src
parent641d87f548c177edbaeecc717eda42323b0e2c7f (diff)
downloademacs-3625e9685c707902ce1ecce411cb97afc75f0405.tar.gz
emacs-3625e9685c707902ce1ecce411cb97afc75f0405.zip
* 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.
Diffstat (limited to 'src')
-rw-r--r--src/nsterm.m92
1 files changed, 42 insertions, 50 deletions
diff --git a/src/nsterm.m b/src/nsterm.m
index 4400dc728db..9c7d35fcfee 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -1346,13 +1346,14 @@ ns_get_color (const char *name, NSColor **col)
1346/* -------------------------------------------------------------------------- 1346/* --------------------------------------------------------------------------
1347 Parse a color name 1347 Parse a color name
1348/* -------------------------------------------------------------------------- 1348/* --------------------------------------------------------------------------
1349/* On *Step, we recognize several color formats, in addition to a catalog 1349/* On *Step, we attempt to mimic the X11 platform here, down to installing an
1350 of colors found in the file Emacs.clr. Color formats include: 1350 X11 rgb.txt-compatible color list in Emacs.clr (see ns_term_init()).
1351 - #rrggbb where rr, gg, bb specify red, green and blue in hex. */ 1351 See: http://thread.gmane.org/gmane.emacs.devel/113050/focus=113272). */
1352{ 1352{
1353 NSColor * new = nil; 1353 NSColor *new = nil;
1354 const char *hex = NULL; 1354 static char hex[20];
1355 enum { rgb } color_space; 1355 int scaling;
1356 float r = -1.0, g, b;
1356 NSString *nsname = [NSString stringWithUTF8String: name]; 1357 NSString *nsname = [NSString stringWithUTF8String: name];
1357 1358
1358/*fprintf (stderr, "ns_get_color: '%s'\n", name); */ 1359/*fprintf (stderr, "ns_get_color: '%s'\n", name); */
@@ -1364,60 +1365,53 @@ ns_get_color (const char *name, NSColor **col)
1364 name = [ns_selection_color UTF8String]; 1365 name = [ns_selection_color UTF8String];
1365 } 1366 }
1366 1367
1367 if (name[0] == '0' || name[0] == '1' || name[0] == '.') 1368 /* First, check for some sort of numeric specification. */
1369 hex[0] = '\0';
1370
1371 if (name[0] == '0' || name[0] == '1' || name[0] == '.') /* RGB decimal */
1368 { 1372 {
1369 /* RGB decimal */
1370 NSScanner *scanner = [NSScanner scannerWithString: nsname]; 1373 NSScanner *scanner = [NSScanner scannerWithString: nsname];
1371 float r, g, b;
1372 [scanner scanFloat: &r]; 1374 [scanner scanFloat: &r];
1373 [scanner scanFloat: &g]; 1375 [scanner scanFloat: &g];
1374 [scanner scanFloat: &b]; 1376 [scanner scanFloat: &b];
1375 *col = [NSColor colorWithCalibratedRed: r green: g blue: b alpha: 1.0];
1376 UNBLOCK_INPUT;
1377 return 0;
1378 } 1377 }
1379 1378 else if (!strncmp(name, "rgb:", 4)) /* A newer X11 format -- rgb:r/g/b */
1380 if (name[0] == '#') /* X11 format */
1381 { 1379 {
1382 hex = name + 1; 1380 strcpy(hex, name + 4);
1383 color_space = rgb; 1381 scaling = (strlen(hex) - 2) / 4;
1382 }
1383 else if (name[0] == '#') /* An old X11 format; convert to newer */
1384 {
1385 int len = (strlen(name) - 1);
1386 int start = (len % 3 == 0) ? 1 : len / 4 + 1;
1387 int i;
1388 scaling = strlen(name+start) / 3;
1389 for (i=0; i<3; i++) {
1390 strncpy(hex + i * (scaling + 1), name + start + i * scaling, scaling);
1391 hex[(i+1) * (scaling + 1) - 1] = '/';
1392 }
1393 hex[3 * (scaling + 1) - 1] = '\0';
1384 } 1394 }
1385 1395
1386 /* Direct colors (hex values) */ 1396 if (hex[0])
1387 if (hex)
1388 { 1397 {
1389 unsigned long long color = 0; 1398 int rr, gg, bb;
1390 if (sscanf (hex, "%x", &color)) 1399 float fscale = scaling == 4 ? 65535.0 : (scaling == 2 ? 255.0 : 15.0);
1400 if (sscanf (hex, "%x/%x/%x", &rr, &gg, &bb))
1391 { 1401 {
1392 float f1, f2, f3, f4; 1402 r = rr / fscale;
1393 /* Assume it's either 1 byte or 2 per channel... */ 1403 g = gg / fscale;
1394 if (strlen(hex) > 8) { 1404 b = bb / fscale;
1395 f1 = ((color >> 48) & 0xffff) / 65535.0;
1396 f2 = ((color >> 32) & 0xffff) / 65535.0;
1397 f3 = ((color >> 16) & 0xffff) / 65535.0;
1398 f4 = ((color ) & 0xffff) / 65535.0;
1399 } else {
1400 f1 = ((color >> 24) & 0xff) / 255.0;
1401 f2 = ((color >> 16) & 0xff) / 255.0;
1402 f3 = ((color >> 8) & 0xff) / 255.0;
1403 f4 = ((color ) & 0xff) / 255.0;
1404 }
1405
1406 switch (color_space)
1407 {
1408 case rgb:
1409 *col = [NSColor colorWithCalibratedRed: f2
1410 green: f3
1411 blue: f4
1412 alpha: 1.0];
1413 break;
1414 }
1415 *col = [*col colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
1416 UNBLOCK_INPUT;
1417 return 0;
1418 } 1405 }
1419 } 1406 }
1420 1407
1408 if (r >= 0.0)
1409 {
1410 *col = [NSColor colorWithCalibratedRed: r green: g blue: b alpha: 1.0];
1411 UNBLOCK_INPUT;
1412 return 0;
1413 }
1414
1421 /* Otherwise, color is expected to be from a list */ 1415 /* Otherwise, color is expected to be from a list */
1422 { 1416 {
1423 NSEnumerator *lenum, *cenum; 1417 NSEnumerator *lenum, *cenum;
@@ -1444,10 +1438,8 @@ ns_get_color (const char *name, NSColor **col)
1444 } 1438 }
1445 } 1439 }
1446 1440
1447 if ( new ) 1441 if (new)
1448 *col = [new colorUsingColorSpaceName: NSCalibratedRGBColorSpace]; 1442 *col = [new colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
1449/* else
1450 NSLog (@"Failed to find color '%@'", nsname); */
1451 UNBLOCK_INPUT; 1443 UNBLOCK_INPUT;
1452 return new ? 0 : 1; 1444 return new ? 0 : 1;
1453} 1445}