You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

123 lines
2.9 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file colour.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Colour handling
  15. *
  16. * This file contains functions for converting colour values between
  17. * various colourspaces.
  18. */
  19. #include "config.h"
  20. #include "cucul.h"
  21. #include "cucul_internals.h"
  22. static const uint16_t ansitab[16] =
  23. {
  24. 0xf000, 0xf008, 0xf080, 0xf088, 0xf800, 0xf808, 0xf880, 0xf888,
  25. 0xf444, 0xf44f, 0xf4f4, 0xf4ff, 0xff44, 0xff4f, 0xfff4, 0xffff,
  26. };
  27. static uint8_t nearest_ansi(uint16_t argb16, uint8_t def)
  28. {
  29. unsigned int i, best, dist;
  30. if(argb16 < CUCUL_COLOR_DEFAULT)
  31. return argb16;
  32. if(argb16 == CUCUL_COLOR_DEFAULT || argb16 == CUCUL_COLOR_TRANSPARENT)
  33. return def;
  34. if(argb16 < 0x5fff) /* too transparent, return default colour */
  35. return def;
  36. best = def;
  37. dist = 0xffff;
  38. for(i = 0; i < 16; i++)
  39. {
  40. unsigned int d = 0;
  41. int a, b;
  42. a = (ansitab[i] >> 8) & 0xf;
  43. b = (argb16 >> 8) & 0xf;
  44. d += (a - b) * (a - b);
  45. a = (ansitab[i] >> 4) & 0xf;
  46. b = (argb16 >> 4) & 0xf;
  47. d += (a - b) * (a - b);
  48. a = ansitab[i] & 0xf;
  49. b = argb16 & 0xf;
  50. d += (a - b) * (a - b);
  51. if(d < dist)
  52. {
  53. dist = d;
  54. best = i;
  55. }
  56. }
  57. return best;
  58. }
  59. uint8_t _cucul_argb32_to_ansi8(uint32_t c)
  60. {
  61. uint16_t fg = c & 0xffff;
  62. uint16_t bg = c >> 16;
  63. return nearest_ansi(fg, CUCUL_COLOR_LIGHTGRAY)
  64. | (nearest_ansi(bg, CUCUL_COLOR_BLACK) << 4);
  65. }
  66. uint8_t _cucul_argb32_to_ansi4fg(uint32_t c)
  67. {
  68. return nearest_ansi(c & 0xffff, CUCUL_COLOR_LIGHTGRAY);
  69. }
  70. uint8_t _cucul_argb32_to_ansi4bg(uint32_t c)
  71. {
  72. return nearest_ansi(c >> 16, CUCUL_COLOR_BLACK);
  73. }
  74. void _cucul_argb32_to_argb4(uint32_t c, uint8_t argb[8])
  75. {
  76. uint16_t fg = c & 0xffff;
  77. uint16_t bg = c >> 16;
  78. if(fg < CUCUL_COLOR_DEFAULT)
  79. fg = ansitab[fg];
  80. else if(fg == CUCUL_COLOR_DEFAULT)
  81. fg = ansitab[CUCUL_COLOR_LIGHTGRAY];
  82. else if(fg == CUCUL_COLOR_TRANSPARENT)
  83. fg = 0x0fff;
  84. if(bg < CUCUL_COLOR_DEFAULT)
  85. bg = ansitab[bg];
  86. else if(bg == CUCUL_COLOR_DEFAULT)
  87. bg = ansitab[CUCUL_COLOR_BLACK];
  88. else if(bg == CUCUL_COLOR_TRANSPARENT)
  89. bg = 0x0fff;
  90. argb[0] = bg >> 12;
  91. argb[1] = (bg >> 8) & 0xf;
  92. argb[2] = (bg >> 4) & 0xf;
  93. argb[3] = bg & 0xf;
  94. argb[4] = fg >> 12;
  95. argb[5] = (fg >> 8) & 0xf;
  96. argb[6] = (fg >> 4) & 0xf;
  97. argb[7] = fg & 0xf;
  98. }