color 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. .TH COLOR 6
  2. .SH NAME
  3. color \- representation of pixels and colors
  4. .SH DESCRIPTION
  5. To address problems of consistency and portability among applications,
  6. Plan 9 uses a fixed color map, called
  7. .BR rgbv ,
  8. on 8-bit-per-pixel displays.
  9. Although this avoids problems caused by multiplexing color maps between
  10. applications, it requires that the color map chosen be suitable for most purposes
  11. and usable for all.
  12. Other systems that use fixed color maps tend to sample the color cube
  13. uniformly, which has advantages\(emmapping from a (red, green, blue) triple
  14. to the color map and back again is easy\(embut ignores an important property
  15. of the human visual system: eyes are
  16. much more sensitive to small changes in intensity than
  17. to changes in hue.
  18. Sampling the color cube uniformly gives a color map with many different
  19. hues, but only a few shades of each.
  20. Continuous tone images converted into such maps demonstrate conspicuous
  21. artifacts.
  22. .PP
  23. Rather than dice the color cube into subregions of
  24. size 6\(mu6\(mu6 (as in Netscape Navigator) or 8\(mu8\(mu4
  25. (as in previous releases of Plan 9), picking 1 color in each,
  26. the
  27. .B rgbv
  28. color map uses a 4\(mu4\(mu4 subdivision, with
  29. 4 shades in each subcube.
  30. The idea is to reduce the color resolution by dicing
  31. the color cube into fewer cells, and to use the extra space to increase the intensity
  32. resolution.
  33. This results in 16 grey shades (4 grey subcubes with
  34. 4 samples in each), 13 shades of each primary and secondary color (3 subcubes
  35. with 4 samples plus black) and a reasonable selection of colors covering the
  36. rest of the color cube.
  37. The advantage is better representation of
  38. continuous tones.
  39. .PP
  40. The following function computes the 256 3-byte entries in the color map:
  41. .IP
  42. .EX
  43. .ta 6n +6n +6n +6n
  44. void
  45. setmaprgbv(uchar cmap[256][3])
  46. {
  47. uchar *c;
  48. int r, g, b, v;
  49. int num, den;
  50. int i, j;
  51. for(r=0,i=0; r!=4; r++)
  52. for(v=0; v!=4; v++,i+=16)
  53. for(g=0,j=v-r; g!=4; g++)
  54. for(b=0; b!=4; b++,j++){
  55. c = cmap[i+(j&15)];
  56. den = r;
  57. if(g > den)
  58. den = g;
  59. if(b > den)
  60. den = b;
  61. if(den == 0) /* would divide check; pick grey shades */
  62. c[0] = c[1] = c[2] = 17*v;
  63. else{
  64. num = 17*(4*den+v);
  65. c[0] = r*num/den;
  66. c[1] = g*num/den;
  67. c[2] = b*num/den;
  68. }
  69. }
  70. }
  71. .EE
  72. .PP
  73. There are 4 nested loops to pick the (red,green,blue) coordinates of the subcube,
  74. and the value (intensity) within the subcube, indexed by
  75. .BR r ,
  76. .BR g ,
  77. .BR b ,
  78. and
  79. .BR v ,
  80. whence
  81. the name
  82. .IR rgbv .
  83. The peculiar order in which the color map is indexed is designed to distribute the
  84. grey shades uniformly through the map\(emthe
  85. .IR i 'th
  86. grey shade,
  87. .RI 0<= i <=15
  88. has index
  89. .IR i ×17,
  90. with black going to 0 and white to 255.
  91. Therefore, when a call to
  92. .B draw
  93. converts a 1, 2 or 4 bit-per-pixel picture to 8 bits per pixel (which it does
  94. by replicating the pixels' bits), the converted pixel values are the appropriate
  95. grey shades.
  96. .PP
  97. The
  98. .B rgbv
  99. map is not gamma-corrected, for two reasons. First, photographic
  100. film and television are both normally under-corrected, the former by an
  101. accident of physics and the latter by NTSC's design.
  102. Second, we require extra color resolution at low intensities because of the
  103. non-linear response and adaptation of the human visual system.
  104. Properly
  105. gamma-corrected displays with adequate low-intensity resolution pack the
  106. high-intensity parts of the color cube with colors whose differences are
  107. almost imperceptible.
  108. Either reason suggests concentrating
  109. the available intensities at the low end of the range.
  110. .PP
  111. On `true-color' displays with separate values for the red, green, and blue
  112. components of a pixel, the values are chosen so 0 represents no intensity (black) and the
  113. maximum value (255 for an 8-bit-per-color display) represents full intensity (e.g., full red).
  114. Common display depths are 24 bits per pixel, with 8 bits per color in order
  115. red, green, blue, and 16 bits per pixel, with 5 bits of red, 6 bits of green, and 5 bits of blue.
  116. .PP
  117. Colors may also be created with an opacity factor called
  118. .BR alpha ,
  119. which is scaled so 0 represents fully transparent and 255 represents opaque color.
  120. The alpha is
  121. .I premultiplied
  122. into the other channels, as described in the paper by Porter and Duff cited in
  123. .IR draw (2).
  124. The function
  125. .B setalpha
  126. (see
  127. .IR allocimage (2))
  128. aids the initialization of color values with non-trivial alpha.
  129. .PP
  130. The packing of pixels into bytes and words is odd.
  131. For compatibility with VGA frame buffers, the bits within a
  132. pixel byte are in big-endian order (leftmost pixel is most
  133. significant bits in byte), while bytes within a pixel are packed in little-endian
  134. order. Pixels are stored in contiguous bytes. This results
  135. in unintuitive pixel formats. For example, for the RGB24 format,
  136. the byte ordering is blue, green, red.
  137. .PP
  138. To maintain a constant external representation,
  139. the
  140. .IR draw (3)
  141. interface
  142. as well as the
  143. various graphics libraries represent colors
  144. by 32-bit numbers, as described in
  145. .IR color (2).
  146. .SH "SEE ALSO"
  147. .IR color (2),
  148. .IR graphics (2),
  149. .IR draw (2)