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.
 
 
 
 
 
 

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