aboutsummaryrefslogtreecommitdiff
path: root/man/man7/color.html
blob: 262e46337b50d5fecc005618a62621884a95d557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<head>
<title>color(7) - Plan 9 from User Space</title>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
</head>
<body bgcolor=#ffffff>
<table border=0 cellpadding=0 cellspacing=0 width=100%>
<tr height=10><td>
<tr><td width=20><td>
<tr><td width=20><td><b>COLOR(7)</b><td align=right><b>COLOR(7)</b>
<tr><td width=20><td colspan=2>
    <br>
<p><font size=+1><b>NAME     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    color &ndash; representation of pixels and colors<br>
    
</table>
<p><font size=+1><b>DESCRIPTION     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    To address problems of consistency and portability among applications,
    Plan 9 uses a fixed color map, called <tt><font size=+1>rgbv</font></tt>, on 8-bit-per-pixel
    displays. Although this avoids problems caused by multiplexing
    color maps between applications, it requires that the color map
    chosen be suitable for most purposes and usable for
    all. Other systems that use fixed color maps tend to sample the
    color cube uniformly, which has advantages--mapping from a (red,
    green, blue) triple to the color map and back again is easy--but
    ignores an important property of the human visual system: eyes
    are much more sensitive to small changes in intensity than
    to changes in hue. Sampling the color cube uniformly gives a color
    map with many different hues, but only a few shades of each. Continuous
    tone images converted into such maps demonstrate conspicuous artifacts.
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    Rather than dice the color cube into subregions of size 6&#215;6&#215;6 (as
    in Netscape Navigator) or 8&#215;8&#215;4 (as in previous releases of Plan
    9), picking 1 color in each, the <tt><font size=+1>rgbv</font></tt> color map uses a 4&#215;4&#215;4 subdivision,
    with 4 shades in each subcube. The idea is to reduce the color
    resolution by dicing the color cube into fewer
    cells, and to use the extra space to increase the intensity resolution.
    This results in 16 grey shades (4 grey subcubes with 4 samples
    in each), 13 shades of each primary and secondary color (3 subcubes
    with 4 samples plus black) and a reasonable selection of colors
    covering the rest of the color cube. The advantage is
    better representation of continuous tones. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    The following function computes the 256 3-byte entries in the
    color map:<br>
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

        <tt><font size=+1>void<br>
        setmaprgbv(uchar cmap[256][3])<br>
        {<br>
         
        <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

            uchar *c;<br>
             int r, g, b, v;<br>
             int num, den;<br>
             int i, j;<br>
             for(r=0,i=0; r!=4; r++)<br>
             for(v=0; v!=4; v++,i+=16)<br>
             for(g=0,j=v&#8722;r; g!=4; g++)<br>
             for(b=0; b!=4; b++,j++){<br>
             c = cmap[i+(j&amp;15)];<br>
             den = r;<br>
             if(g &gt; den)<br>
             den = g;<br>
             if(b &gt; den)<br>
             den = b;<br>
             if(den == 0) /* would divide check; pick grey shades */<br>
             c[0] = c[1] = c[2] = 17*v;<br>
             else{<br>
             num = 17*(4*den+v);<br>
             c[0] = r*num/den;<br>
             c[1] = g*num/den;<br>
             c[2] = b*num/den;<br>
             }<br>
             }<br>
            
        </table>
        }<br>
        
        <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
        </font></tt>
        
    </table>
    There are 4 nested loops to pick the (red,green,blue) coordinates
    of the subcube, and the value (intensity) within the subcube,
    indexed by <tt><font size=+1>r</font></tt>, <tt><font size=+1>g</font></tt>, <tt><font size=+1>b</font></tt>, and <tt><font size=+1>v</font></tt>, whence the name <i>rgbv</i>. The peculiar
    order in which the color map is indexed is designed to distribute
    the grey shades uniformly through the map--the <i>i</i>&#8217;th grey
    shade, 0&lt;=<i>i</i>&lt;=15 has index <i>i</i>x17, with black going to 0 and white to
    255. Therefore, when a call to <tt><font size=+1>draw</font></tt> converts a 1, 2 or 4 bit-per-pixel
    picture to 8 bits per pixel (which it does by replicating the
    pixels&#8217; bits), the converted pixel values are the appropriate
    grey shades. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    The <tt><font size=+1>rgbv</font></tt> map is not gamma-corrected, for two reasons. First, photographic
    film and television are both normally under-corrected, the former
    by an accident of physics and the latter by NTSC&#8217;s design. Second,
    we require extra color resolution at low intensities because of
    the non-linear response and adaptation of
    the human visual system. Properly gamma-corrected displays with
    adequate low-intensity resolution pack the high-intensity parts
    of the color cube with colors whose differences are almost imperceptible.
    Either reason suggests concentrating the available intensities
    at the low end of the range. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    On &#8216;true-color&#8217; displays with separate values for the red, green,
    and blue components of a pixel, the values are chosen so 0 represents
    no intensity (black) and the maximum value (255 for an 8-bit-per-color
    display) represents full intensity (e.g., full red). Common display
    depths are 24 bits per pixel, with 8 bits per
    color in order red, green, blue, and 16 bits per pixel, with 5
    bits of red, 6 bits of green, and 5 bits of blue. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    Colors may also be created with an opacity factor called <tt><font size=+1>alpha</font></tt>,
    which is scaled so 0 represents fully transparent and 255 represents
    opaque color. The alpha is <i>premultiplied</i> into the other channels,
    as described in the paper by Porter and Duff cited in <a href="../man3/draw.html"><i>draw</i>(3)</a>.
    The function <tt><font size=+1>setalpha</font></tt> (see <a href="../man3/allocimage.html"><i>allocimage</i>(3)</a>) aids the
    initialization of color values with non-trivial alpha. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    The packing of pixels into bytes and words is odd. For compatibility
    with VGA frame buffers, the bits within a pixel byte are in big-endian
    order (leftmost pixel is most significant bits in byte), while
    bytes within a pixel are packed in little-endian order. Pixels
    are stored in contiguous bytes. This results in unintuitive
    pixel formats. For example, for the RGB24 format, the byte ordering
    is blue, green, red. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    To maintain a constant external representation, the <a href="../man3/draw.html"><i>draw</i>(3)</a> interface
    as well as the various graphics libraries represent colors by
    32-bit numbers, as described in <a href="../man3/color.html"><i>color</i>(3)</a>.<br>
    
</table>
<p><font size=+1><b>SEE ALSO     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    <a href="../man3/color.html"><i>color</i>(3)</a>, <a href="../man3/graphics.html"><i>graphics</i>(3)</a>, <a href="../man3/draw.html"><i>draw</i>(3)</a><br>
    
</table>

<td width=20>
<tr height=20><td>
</table>
<!-- TRAILER -->
<table border=0 cellpadding=0 cellspacing=0 width=100%>
<tr height=15><td width=10><td><td width=10>
<tr><td><td>
<center>
<a href="../../"><img src="../../dist/spaceglenda100.png" alt="Space Glenda" border=1></a>
</center>
</table>
<!-- TRAILER -->
</body></html>