teken.c (11d38a5764295585a2472d5e861fa8abe1a11eb2) | teken.c (2610c9f2b2449c968304f48bf73d1eef2f4be558) |
---|---|
1/*- 2 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 438 unchanged lines hidden (view full) --- 447 /* Also count the last argument. */ 448 t->t_curnum++; 449 } 450 } 451 452 return (0); 453} 454 | 1/*- 2 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 438 unchanged lines hidden (view full) --- 447 /* Also count the last argument. */ 448 t->t_curnum++; 449 } 450 } 451 452 return (0); 453} 454 |
455#define k TC_BLACK 456#define b TC_BLUE 457#define y TC_BROWN 458#define c TC_CYAN 459#define g TC_GREEN 460#define m TC_MAGENTA 461#define r TC_RED 462#define w TC_WHITE 463#define K (TC_BLACK | TC_LIGHT) 464#define B (TC_BLUE | TC_LIGHT) 465#define Y (TC_BROWN | TC_LIGHT) 466#define C (TC_CYAN | TC_LIGHT) 467#define G (TC_GREEN | TC_LIGHT) 468#define M (TC_MAGENTA | TC_LIGHT) 469#define R (TC_RED | TC_LIGHT) 470#define W (TC_WHITE | TC_LIGHT) 471 472/** 473 * The xterm-256 color map has steps of 0x28 (in the range 0-0xff), except 474 * for the first step which is 0x5f. Scale to the range 0-6 by dividing 475 * by 0x28 and rounding down. The range of 0-5 cannot represent the 476 * larger first step. 477 * 478 * This table is generated by the follow rules: 479 * - if all components are equal, the result is black for (0, 0, 0) and 480 * (2, 2, 2), else white; otherwise: 481 * - subtract the smallest component from all components 482 * - if this gives only one nonzero component, then that is the color 483 * - else if one component is 2 or more larger than the other nonzero one, 484 * then that component gives the color 485 * - else there are 2 nonzero components. The color is that of a small 486 * equal mixture of these components (cyan, yellow or magenta). E.g., 487 * (0, 5, 6) (Turquoise2) is a much purer cyan than (0, 2, 3) 488 * (DeepSkyBlue4), but we map both to cyan since we can't represent 489 * delicate shades of either blue or cyan and blue would be worse. 490 * Here it is important that components of 1 never occur. Blue would 491 * be twice as large as green in (0, 1, 2). 492 */ 493static const teken_color_t teken_256to8tab[] = { 494 /* xterm-16+ 8 dark colors: */ 495 0, r, g, y, b, m, c, w, 496 497 /* xterm-16+ 8 light colors: */ 498 0, R, G, Y, B, M, C, W, 499 500 /* Red0 submap. */ 501 k, b, b, b, b, b, 502 g, c, c, b, b, b, 503 g, c, c, c, b, b, 504 g, g, c, c, c, b, 505 g, g, g, c, c, c, 506 g, g, g, g, c, c, 507 508 /* Red2 submap. */ 509 r, m, m, b, b, b, 510 y, k, b, b, b, b, 511 y, g, c, c, b, b, 512 g, g, c, c, c, b, 513 g, g, g, c, c, c, 514 g, g, g, g, c, c, 515 516 /* Red3 submap. */ 517 r, m, m, m, b, b, 518 y, r, m, m, b, b, 519 y, y, w, b, b, b, 520 y, y, g, c, c, b, 521 g, g, g, c, c, c, 522 g, g, g, g, c, c, 523 524 /* Red4 submap. */ 525 r, r, m, m, m, b, 526 r, r, m, m, m, b, 527 y, y, r, m, m, b, 528 y, y, y, w, b, b, 529 y, y, y, g, c, c, 530 g, g, g, g, c, c, 531 532 /* Red5 submap. */ 533 r, r, r, m, m, m, 534 r, r, r, m, m, m, 535 r, r, r, m, m, m, 536 y, y, y, r, m, m, 537 y, y, y, y, w, b, 538 y, y, y, y, g, c, 539 540 /* Red6 submap. */ 541 r, r, r, r, m, m, 542 r, r, r, r, m, m, 543 r, r, r, r, m, m, 544 r, r, r, r, m, m, 545 y, y, y, y, r, m, 546 y, y, y, y, y, w, 547 548 /* Grey submap. */ 549 k, k, k, k, k, k, 550 k, k, k, k, k, k, 551 w, w, w, w, w, w, 552 w, w, w, w, w, w, 553}; 554 555/* 556 * This table is generated from the previous one by setting TC_LIGHT for 557 * entries whose luminosity in the xterm256 color map is 60% or larger. 558 * Thus the previous table is currently not really needed. It will be 559 * used for different fine tuning of the tables. 560 */ 561static const teken_color_t teken_256to16tab[] = { 562 /* xterm-16+ 8 dark colors: */ 563 0, r, g, y, b, m, c, w, 564 565 /* xterm-16+ 8 light colors: */ 566 0, R, G, Y, B, M, C, W, 567 568 /* Red0 submap. */ 569 k, b, b, b, b, b, 570 g, c, c, b, b, b, 571 g, c, c, c, b, b, 572 g, g, c, c, c, b, 573 g, g, g, c, c, c, 574 g, g, g, g, c, c, 575 576 /* Red2 submap. */ 577 r, m, m, b, b, b, 578 y, K, b, b, B, B, 579 y, g, c, c, B, B, 580 g, g, c, c, C, B, 581 g, G, G, C, C, C, 582 g, G, G, G, C, C, 583 584 /* Red3 submap. */ 585 r, m, m, m, b, b, 586 y, r, m, m, B, B, 587 y, y, w, B, B, B, 588 y, y, G, C, C, B, 589 g, G, G, C, C, C, 590 g, G, G, G, C, C, 591 592 /* Red4 submap. */ 593 r, r, m, m, m, b, 594 r, r, m, m, M, B, 595 y, y, R, M, M, B, 596 y, y, Y, W, B, B, 597 y, Y, Y, G, C, C, 598 g, G, G, G, C, C, 599 600 /* Red5 submap. */ 601 r, r, r, m, m, m, 602 r, R, R, M, M, M, 603 r, R, R, M, M, M, 604 y, Y, Y, R, M, M, 605 y, Y, Y, Y, W, B, 606 y, Y, Y, Y, G, C, 607 608 /* Red6 submap. */ 609 r, r, r, r, m, m, 610 r, R, R, R, M, M, 611 r, R, R, R, M, M, 612 r, R, R, R, M, M, 613 y, Y, Y, Y, R, M, 614 y, Y, Y, Y, Y, W, 615 616 /* Grey submap. */ 617 k, k, k, k, k, k, 618 K, K, K, K, K, K, 619 w, w, w, w, w, w, 620 W, W, W, W, W, W, 621}; 622 623#undef k 624#undef b 625#undef y 626#undef c 627#undef g 628#undef m 629#undef r 630#undef w 631#undef K 632#undef B 633#undef Y 634#undef C 635#undef G 636#undef M 637#undef R 638#undef W 639 |
|
455teken_color_t 456teken_256to8(teken_color_t c) 457{ | 640teken_color_t 641teken_256to8(teken_color_t c) 642{ |
458 unsigned int r, g, b; | |
459 | 643 |
460 if (c < 16) { 461 /* Traditional color indices. */ 462 return (c % 8); 463 } else if (c >= 244) { 464 /* Upper grayscale colors. */ 465 return (TC_WHITE); 466 } else if (c >= 232) { 467 /* Lower grayscale colors. */ 468 return (TC_BLACK); 469 } | 644 return (teken_256to8tab[c % 256]); 645} |
470 | 646 |
471 /* Convert to RGB. */ 472 c -= 16; 473 b = c % 6; 474 g = (c / 6) % 6; 475 r = c / 36; | 647teken_color_t 648teken_256to16(teken_color_t c) 649{ |
476 | 650 |
477 if (r < g) { 478 /* Possibly green. */ 479 if (g < b) 480 return (TC_BLUE); 481 else if (g > b) 482 return (TC_GREEN); 483 else 484 return (TC_CYAN); 485 } else if (r > g) { 486 /* Possibly red. */ 487 if (r < b) 488 return (TC_BLUE); 489 else if (r > b) 490 return (TC_RED); 491 else 492 return (TC_MAGENTA); 493 } else { 494 /* Possibly brown. */ 495 if (g < b) 496 return (TC_BLUE); 497 else if (g > b) 498 return (TC_BROWN); 499 else if (r < 3) 500 return (TC_BLACK); 501 else 502 return (TC_WHITE); 503 } | 651 return (teken_256to16tab[c % 256]); |
504} 505 506static const char * const special_strings_cons25[] = { 507 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B", 508 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C", 509 510 [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F", 511 [TKEY_INSERT] = "\x1B[L", [TKEY_DELETE] = "\x7F", --- 55 unchanged lines hidden --- | 652} 653 654static const char * const special_strings_cons25[] = { 655 [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B", 656 [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C", 657 658 [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F", 659 [TKEY_INSERT] = "\x1B[L", [TKEY_DELETE] = "\x7F", --- 55 unchanged lines hidden --- |