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.
 
 
 
 
 
 

74 lines
1.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. * 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. /* FIXME: could this be inlined? */
  28. uint8_t _cucul_argb32_to_ansi8(uint32_t c)
  29. {
  30. /* FIXME: we need nearest colour handling for non-ANSI */
  31. return (c & 0x0000000f) | ((c & 0x000f0000) >> 12);
  32. }
  33. uint8_t _cucul_argb32_to_ansi4fg(uint32_t c)
  34. {
  35. /* FIXME: we need nearest colour handling for non-ANSI */
  36. return c & 0x0000000f;
  37. }
  38. uint8_t _cucul_argb32_to_ansi4bg(uint32_t c)
  39. {
  40. /* FIXME: we need nearest colour handling for non-ANSI */
  41. return (c & 0x000f0000) >> 16;
  42. }
  43. void _cucul_argb32_to_argb4(uint32_t c, uint8_t argb[8])
  44. {
  45. uint16_t fg = c & 0xffff;
  46. uint16_t bg = c >> 16;
  47. if(fg < 0x0016)
  48. fg = ansitab[fg];
  49. if(bg < 0x0016)
  50. bg = ansitab[bg];
  51. argb[0] = bg >> 12;
  52. argb[1] = (bg >> 8) & 0xf;
  53. argb[2] = (bg >> 4) & 0xf;
  54. argb[3] = bg & 0xf;
  55. argb[4] = fg >> 12;
  56. argb[5] = (fg >> 8) & 0xf;
  57. argb[6] = (fg >> 4) & 0xf;
  58. argb[7] = fg & 0xf;
  59. }