rgbrgbv.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. /*
  13. * This version of closest() is now (feb 20, 2001) installed as rgb2cmap in libdraw
  14. */
  15. int
  16. closest(int cr, int cg, int cb)
  17. {
  18. int i, r, g, b, sq;
  19. uint32_t rgb;
  20. int best, bestsq;
  21. best = 0;
  22. bestsq = 0x7FFFFFFF;
  23. for(i=0; i<256; i++){
  24. rgb = cmap2rgb(i);
  25. r = (rgb>>16) & 0xFF;
  26. g = (rgb>>8) & 0xFF;
  27. b = (rgb>>0) & 0xFF;
  28. sq = (r-cr)*(r-cr)+(g-cg)*(g-cg)+(b-cb)*(b-cb);
  29. if(sq < bestsq){
  30. bestsq = sq;
  31. best = i;
  32. }
  33. }
  34. return best;
  35. }
  36. void
  37. main(int argc, char *argv[])
  38. {
  39. int i, rgb;
  40. int r, g, b;
  41. uint8_t close[16*16*16];
  42. /* rgbmap */
  43. print("uint rgbmap[256] = {\n");
  44. for(i=0; i<256; i++){
  45. if(i%8 == 0)
  46. print("\t");
  47. rgb = cmap2rgb(i);
  48. r = (rgb>>16) & 0xFF;
  49. g = (rgb>>8) & 0xFF;
  50. b = (rgb>>0) & 0xFF;
  51. print("0x%.6ulX, ", (r<<16) | (g<<8) | b);
  52. if(i%8 == 7)
  53. print("\n");
  54. }
  55. print("};\n\n");
  56. /* closestrgb */
  57. print("uchar closestrgb[16*16*16] = {\n");
  58. for(r=0; r<256; r+=16)
  59. for(g=0; g<256; g+=16)
  60. for(b=0; b<256; b+=16)
  61. close[(b/16)+16*((g/16)+16*(r/16))] = closest(r+8, g+8, b+8);
  62. for(i=0; i<16*16*16; i++){
  63. if(i%16 == 0)
  64. print("\t");
  65. print("%d,", close[i]);
  66. if(i%16 == 15)
  67. print("\n");
  68. }
  69. print("};\n\n");
  70. exits(nil);
  71. }