While studying the viewmat lab today and experimenting with various ways of displaying numbers as colors, I defined a short phrase using viewmat which displays all 216 "web-safe" colors:
load 'viewmat' (51 * (6 (&#.)(^:_1) i. 216)) viewmat 12 18 $ i. 216 This phrase is explained in detail below. It uses the dyadic version of viewmat demonstrated in the lab, and the subphrase: (&#.)(^:_1) which is a dyad returning the base x representation of decimal integer y. Argument x to dyad viewmat is a color table represented as a 216 by 3 array. It can be thought of as a "1-frame" of 3-item arrays ("1-cells"), where each of the three integer items of each 1-cell is one of the six integers in the range 0 and 255 (inclusive) which divide this range evenly into 6 equal intervals (i.e., 0, 51, 102, 153, 204, 255). Each of these three-item "1-cells" in the 216-item color table represents the red, green, and blue (RGB) components of a color in the color table containing 6 ^ 3 = 216 colors distributed evenly between black (0 0 0) and white (255 255 255). Historically, web-designers used to call these 216 colors the "web-safe" colors - because they could be displayed on older 8-bit (256-color) displays using the color palettes of all popular browsers without resorting to "dithering": http://en.wikipedia.org/wiki/Web_colors#Web-safe_colors With most modern displays (even on many mobile devices) nowadays having 16- or even 24-bit color depth, we no longer need to use "web-safe" colors. But trying to define a phrase using the viewmat dyad to display 216 colors distributed evenly between black and white turned out to be an interesting exercise, which led me to discover several verbs for representing decimal numbers in arbitrary bases defined in the Phrases documentation: http://www.jsoftware.com/help/phrases/representations.htm +++ Part 6 of 20 in the viewmat lab loads a script: system\packages\color\color16.ijs which defines a noun COLOR16 containing a color table containing 16 HTML primary colors: COLOR16=: ] ;._2 (0 : 0) Aqua=: 0 255 255 Black=: 0 0 0 Blue=: 0 0 255 Fuchsia=: 255 0 255 Gray=: 128 128 128 Green=: 0 128 0 Lime=: 0 255 0 Maroon=: 128 0 0 Navy=: 0 0 128 Olive=: 128 128 0 Purple=: 128 0 128 Red=: 255 0 0 Silver=: 192 192 192 Teal=: 0 128 128 White=: 255 255 255 Yellow=: 255 255 0 ) (As a newbie, I'm not sure what the syntax: ] ;,_2 means. But the above expression does define a noun COLOR16 containing a color table suitable for use as argument x to dyad viewmat.) Now a larger color table for 216 colors would be an array of 216 3-item integer arrays ("1-cells"), where each of the three integer items of each 1-cell ranges from 0 to 255 as before. We can assume that the RGB values are distributed evenly from black =: 0 0 0 to white =: 255 255 255. The desired 216-item color table to be used as argument x to dyad viewmat can then be computed as follows: x.1 -- We first to compute all three-element combinations from a set of six elements (0, 1, 2, 3, 4, 5). x.2 -- Note that 216 = 6 ^ 3. So we can represent each number 0, 1, 2, ... 215 as a three-item list consisting of the representation of the number in base 6, and this will be all three-element combinations from a set of six items. To represent decimal numbers in base 6, we need a phrase generalizing J's "Antibase Two" function: http://www.jsoftware.com/help/dictionary/d402.htm Several such phrases are provided here: http://www.jsoftware.com/help/phrases/representations.htm We can choose the dyadic verb named a16 which returns the base x representation of decimal integer y: a16=: (&#.) (^:_1) For example, the expression: 6 (&#.)(^:_1) i. 216 returns the numbers 0, 1, 2 ... 215 in base 6 (each represented as a three-item integer array): 0 0 0 0 0 1 0 0 2 0 0 3 0 0 4 0 0 5 0 1 0 0 1 1 *... (200 rows omitted)* 5 4 4 5 4 5 5 5 0 5 5 1 5 5 2 5 5 3 5 5 4 5 5 5 x.3 -- We want each of R, G and B to range over six values distributed evenly between 0 and 255 (inclusive), which will yield 6 ^ 3 = 216 colors distributed evenly between black (0 0 0) and white (255 255 255). To obtain the desired 216-item color table with items distributed evenly from 0 0 0 to 255, multiply the above 216-row table (representing all 216 three-digit numbers in base 6) by 255 % 5 = 51: 51 * (6 (&#.)(^:_1) i. 216) returning: 0 0 0 0 0 51 0 0 102 0 0 153 0 0 204 0 0 255 0 51 0 0 51 51 *... (200 rows omitted)* 255 204 204 255 204 255 255 255 0 255 255 51 255 255 102 255 255 153 255 255 204 255 255 255 which can now be used as argument x to the dyadic verb viewmat. y -- For argument y to dyad viewmat, any convenient rectangular layout of the numbers from 0 to 215 could be used. Since the prime factors of 216 are 2 2 2 3 3 3, a layout choice with reasonable proportions for displaying comfortably on a landscape-oriented monitor might be 2 * 2 * 3 = 12 rows, and 3 * 3 * 2 = 18 columns, which gives the following choice for argument x to the viewmat dyad: 12 18 $ i. 216 Applying the viewmat dyad to these arguments: (51 * (6 (&#.)(^:_1) i. 216)) viewmat 12 18 $ i. 216 opens a new window displaying 216 colors distributed evenly from black (0 0 0) to white (255 255 255) in a 12 by 18 layout. Note that the factor 51 used in the definition of the color table occurred as a consequence of the assumption that the the 216 colors from black (0 0 0) to white (255 255 255) should be evenly distributed (i.e., on equal intervals). This is exactly how the 216 "web-safe" colors are defined: with the red, green and blue components each ranging from 0 to 255 in steps of 51, taking on the values 0, 51, 102, 153, 204, and 255: http://en.wikipedia.org/wiki/Web_colors#Web-safe_colors So the 216 colors distributed evenly from black to white displayed by the above phrase are indeed the 216 "web-safe" colors. (I'd like to add that it's a real pleasure to start using a high-performance interpreted function-level language like J and its IDE, where only a few days after downloading it I already feel comfortable enough to start contributing a simple "how-to" like this to the forum - unlike nearly all the other languages and IDEs I've tried, which are so complicated and bloated and slow that I waste days or weeks getting nothing done at all except posting mainly desperate "cries for help" to their forums, unable to get even the most basic features installed and configured. After only a few days of using J I'm already starting to feel productive. Thank you for liberating me from the "von Neumann programming style"!) http://en.wikipedia.org/wiki/Function-level_programming http://en.wikipedia.org/wiki/Von_Neumann_programming_languages +++ +++ +++ ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm