Bill; My instinct would be to first of all transform the RGB into HSV values and then reduce those values with a greater number of values to describe Hue.
RGB to HSV is very fast. David -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of bill lam Sent: Friday, March 31, 2006 2:57 AM To: Programming forum Subject: [Jprogramming] dithering algorithm The following is a simple-minded dithering used in Tara to choose the nearest color from a set of 64 predefined color palette. NB. for biff8 RGB values RGB=: 3 : 0"1 (0{y.) (23 b.) 8 (33 b.) (1{y.) (23 b.) 8 (33 b.) (2{y.) ) RGBtuple=: 3 : 0"0 (16bff (17 b.) y.), (_8 (33 b.) 16bff00 (17 b.) y.), (_16 (33 b.) 16bff0000 (17 b.) y.) ) NB. match color (red, green, blue) to internal color palette table NB. return color index rgbcolor=: 3 : 0 (i. <./) +/"1 | y. -"1 RGBtuple colorset ) NB. predefine black white red green blue yellow magenta cyan colorset=: 0 16bffffff 16bff 16bff00 16bff0000 16bffff 16bff00ff 16bffff00 NB. Sets extra colour palette to the Excel 97+ default. colorset=: colorset, RGB 16b00 16b00 16b00 NB. 8 colorset=: colorset, RGB 16bff 16bff 16bff NB. 9 colorset=: colorset, RGB 16bff 16b00 16b00 NB. 10 colorset=: colorset, RGB 16b00 16bff 16b00 NB. 11 colorset=: colorset, RGB 16b00 16b00 16bff NB. 12 colorset=: colorset, RGB 16bff 16bff 16b00 NB. 13 colorset=: colorset, RGB 16bff 16b00 16bff NB. 14 colorset=: colorset, RGB 16b00 16bff 16bff NB. 15 colorset=: colorset, RGB 16b80 16b00 16b00 NB. 16 colorset=: colorset, RGB 16b00 16b80 16b00 NB. 17 colorset=: colorset, RGB 16b00 16b00 16b80 NB. 18 colorset=: colorset, RGB 16b80 16b80 16b00 NB. 19 colorset=: colorset, RGB 16b80 16b00 16b80 NB. 20 colorset=: colorset, RGB 16b00 16b80 16b80 NB. 21 colorset=: colorset, RGB 16bc0 16bc0 16bc0 NB. 22 colorset=: colorset, RGB 16b80 16b80 16b80 NB. 23 colorset=: colorset, RGB 16b99 16b99 16bff NB. 24 colorset=: colorset, RGB 16b99 16b33 16b66 NB. 25 colorset=: colorset, RGB 16bff 16bff 16bcc NB. 26 colorset=: colorset, RGB 16bcc 16bff 16bff NB. 27 colorset=: colorset, RGB 16b66 16b00 16b66 NB. 28 colorset=: colorset, RGB 16bff 16b80 16b80 NB. 29 colorset=: colorset, RGB 16b00 16b66 16bcc NB. 30 colorset=: colorset, RGB 16bcc 16bcc 16bff NB. 31 colorset=: colorset, RGB 16b00 16b00 16b80 NB. 32 colorset=: colorset, RGB 16bff 16b00 16bff NB. 33 colorset=: colorset, RGB 16bff 16bff 16b00 NB. 34 colorset=: colorset, RGB 16b00 16bff 16bff NB. 35 colorset=: colorset, RGB 16b80 16b00 16b80 NB. 36 colorset=: colorset, RGB 16b80 16b00 16b00 NB. 37 colorset=: colorset, RGB 16b00 16b80 16b80 NB. 38 colorset=: colorset, RGB 16b00 16b00 16bff NB. 39 colorset=: colorset, RGB 16b00 16bcc 16bff NB. 40 colorset=: colorset, RGB 16bcc 16bff 16bff NB. 41 colorset=: colorset, RGB 16bcc 16bff 16bcc NB. 42 colorset=: colorset, RGB 16bff 16bff 16b99 NB. 43 colorset=: colorset, RGB 16b99 16bcc 16bff NB. 44 colorset=: colorset, RGB 16bff 16b99 16bcc NB. 45 colorset=: colorset, RGB 16bcc 16b99 16bff NB. 46 colorset=: colorset, RGB 16bff 16bcc 16b99 NB. 47 colorset=: colorset, RGB 16b33 16b66 16bff NB. 48 colorset=: colorset, RGB 16b33 16bcc 16bcc NB. 49 colorset=: colorset, RGB 16b99 16bcc 16b00 NB. 50 colorset=: colorset, RGB 16bff 16bcc 16b00 NB. 51 colorset=: colorset, RGB 16bff 16b99 16b00 NB. 52 colorset=: colorset, RGB 16bff 16b66 16b00 NB. 53 colorset=: colorset, RGB 16b66 16b66 16b99 NB. 54 colorset=: colorset, RGB 16b96 16b96 16b96 NB. 55 colorset=: colorset, RGB 16b00 16b33 16b66 NB. 56 colorset=: colorset, RGB 16b33 16b99 16b66 NB. 57 colorset=: colorset, RGB 16b00 16b33 16b00 NB. 58 colorset=: colorset, RGB 16b33 16b33 16b00 NB. 59 colorset=: colorset, RGB 16b99 16b33 16b00 NB. 60 colorset=: colorset, RGB 16b99 16b33 16b66 NB. 61 colorset=: colorset, RGB 16b33 16b33 16b99 NB. 62 colorset=: colorset, RGB 16b33 16b33 16b33 NB. 63 For example rgbcolor 0 0 255 4 rgbcolor 200 100 50 53 so it return an index of palette, but for some color rgbcolor 220 220 220 31 RGBtuple 31{colorset 204 204 255 The problem is that original color is grey (no chroma), but it choose a purple color indicating the choice is unwise. Any better algorithm suggestion? ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
