vfs: check userland buffers before reading them.
[haiku.git] / docs / develop / servers / app_server / ColorUtils.htm
blob72d1e407b3f3f44c5beaf11bb29904373ff241b2
1 <HTML>
2 <HEAD>
3 <TITLE>ColorUtils.htm</TITLE>
4 <style type="text/css">
5 <!--
6 .Default {background-color: rgb(255,255,255); color: rgb(0,0,0); font-family: 'Dutch801 Rm BT'; font-size: 12pt}
7 .OBOS-Function-Def {background-color: rgb(255,255,255); color: rgb(0,0,0); font-family: 'Dutch801 Rm BT'; font-size: 16pt}
8 .OBOS-Title {background-color: rgb(255,255,255); color: rgb(0,128,0); font-family: 'Dutch801 Rm BT'; font-size: 24pt}
9 .Text-Background {background-color: rgb(255,255,255)}
10 .GR-Default {}
11 .Body {margin: 0px}
12 .Footer {margin: 0px}
13 .Header {margin: 0px}
14 .WP-Default {text-align: left; text-indent: 0px; margin-left: 0px; margin-right: 0px}
15 -->
16 </style>
17 </HEAD>
18 <BODY BGCOLOR="#ffffff">
19 <DIV class="sheet" id="Sheet 1">
20 <P class="Body" style="margin: 0px"><span class="OBOS-Title">ColorUtils</span><span style="color: rgb(0,0,0); font-size: 24pt"></span></P>
21 <P class="Body" style="margin: 0px"><BR>
22 </P>
23 <P class="Body" style="margin: 0px">These functions are used for general purpose color-related tasks.</P>
24 <P class="Body" style="margin: 0px"><BR>
25 <BR>
26 <HR>
27 </P>
28 <P class="Body" style="margin: 0px">Global Functions</P>
29 <P class="Body" style="margin: 0px"><BR>
30 void SetRGBColor32(rgb_color *col, uint8 r, uint8 g, uint8 b, uint8 a=255)</P>
31 <P class="Body" style="margin: 0px">void SetRGBAColor32(rgb_color *col, uint16 color16)</P>
32 <P class="Body" style="margin: 0px">void SetRGBColor16(uint16 *col, uint8 r, uint8 g, uint8 b)</P>
33 <P class="Body" style="margin: 0px">void SetRGBAColor15(uint16 *col, uint8 r, uint8 g, uint8 b, bool opaque=true)</P>
34 <P class="Body" style="margin: 0px"><BR>
35 uint8 FindClosestColor(rgb_color *palette,rgb_color col)</P>
36 <P class="Body" style="margin: 0px">uint16 FindClosestColor16(rgb_color col)</P>
37 <P class="Body" style="margin: 0px">uint16 FindClosestColor15(rgb_color col)</P>
38 <P class="Body" style="margin: 0px"><BR>
39 rgb_color MakeBlendColor(rgb_color col, rgb_color col2, float position)</P>
40 <P class="Body" style="margin: 0px"><BR>
41 <HR>
42 </P>
43 <P class="Body" style="margin: 0px"><span class="OBOS-Function-Def">void SetRGBColor32(rgb_color *col, uint8 r, uint8 g, uint8 b, uint8 a=255)</span></P>
44 <P class="Body" style="margin: 0px"><BR>
45 Simply assigns the passed parameters to the internal members of the passed color</P>
46 <P class="Body" style="margin: 0px"><BR>
47 <BR>
48 <span class="OBOS-Function-Def">void SetRGBAColor32(rgb_color *col, uint16 color16)</span></P>
49 <P class="Body" style="margin: 0px"><BR>
50 Maps a 16-bit color to a 32-bit one.</P>
51 <P class="Body" style="margin: 0px"><BR>
52 gggbbbbb arrrrrgg</P>
53 <P class="Body" style="margin: 0px">1) Extract component values using the following calculations:</P>
54 <P class="Body" style="margin: 0px"><BR>
55 red16 = (uint8[1] &amp; 124) &gt;&gt; 2</P>
56 <P class="Body" style="margin: 0px"><BR>
57 green16 = ((uint8[0] &amp; 224) &gt;&gt; 5) | ((uint8[1] &amp; 3) &lt;&lt; 3)</P>
58 <P class="Body" style="margin: 0px"><BR>
59 blue16 = uint8[0] &amp; 31</P>
60 <P class="Body" style="margin: 0px">2) Use cross-multiplication to map each 16-bit color component from 0-31 space to 0-255 space, i.e. red32 = (red16 / 31) * 255</P>
61 <P class="Body" style="margin: 0px">3) Assign mapped values to the rgb_color passed to the function</P>
62 <P class="Body" style="margin: 0px"><BR>
63 <BR>
64 <span class="OBOS-Function-Def">void SetRGBColor16(uint16 *col, uint8 r, uint8 g, uint8 b)</span></P>
65 <P class="Body" style="margin: 0px"><BR>
66 Used for easy assignment of opaque (B_RGB16) 16-bit colors.</P>
67 <P class="Body" style="margin: 0px"><BR>
68 1) Clip parameters via a bitwise AND with 31 (var &amp;=31)</P>
69 <P class="Body" style="margin: 0px">2) Create a uint8 * to the passed color</P>
70 <P class="Body" style="margin: 0px">3) Assign as follows and return:</P>
71 <P class="Body" style="margin: 0px"> a) uint8[0] = ( (g &amp; 7) &lt;&lt; 5) | (b &amp; 31)</P>
72 <P class="Body" style="margin: 0px"> b) uint8[1] = ( (r &amp; 31) &lt;&lt; 3) | ( (g &amp; 56) &gt;&gt; 3)</P>
73 <P class="Body" style="margin: 0px"><BR>
74 <BR>
75 <span class="OBOS-Function-Def">void SetRGBAColor15(uint16 *col, uint8 r, uint8 g, uint8 b, bool opaque=true)</span></P>
76 <P class="Body" style="margin: 0px"><BR>
77 Used for easy assignment of alpha-aware (B_RGBA16) 16-bit colors.</P>
78 <P class="Body" style="margin: 0px"><BR>
79 1) Clip parameters via a bitwise AND with 31 (var &amp;=31)</P>
80 <P class="Body" style="margin: 0px">2) Create a uint8 * to the passed color</P>
81 <P class="Body" style="margin: 0px">3) Assign as follows and return:</P>
82 <P class="Body" style="margin: 0px"> a) uint8[0] = ( (g &amp; 7) &lt;&lt; 5) | (b &amp; 31)</P>
83 <P class="Body" style="margin: 0px"> b) uint8[1] = ( (r &amp; 31) &lt;&lt; 2) | ( (g &amp; 24) &gt;&gt; 3) | (a) ? 128 : 0</P>
84 <P class="Body" style="margin: 0px"><BR>
85 <BR>
86 <span class="OBOS-Function-Def">uint8 FindClosestColor(rgb_color *palette,rgb_color col)</span></P>
87 <P class="Body" style="margin: 0px"><BR>
88 Finds the color which most closely resembles the given one in the given palette.</P>
89 <P class="Body" style="margin: 0px"><BR>
90 1) Set the saved delta value to 765 (maximum difference)</P>
91 <P class="Body" style="margin: 0px">2) Loop through all the colors in the palette. For each color,</P>
92 <P class="Body" style="margin: 0px"> a) calculate the delta value for each color component and add them together</P>
93 <P class="Body" style="margin: 0px"> b) compare the new combined delta with the saved one</P>
94 <P class="Body" style="margin: 0px"> c) if the delta is 0, immediately return the current index</P>
95 <P class="Body" style="margin: 0px"> d) if the new one is smaller, save it and also the palette index</P>
96 <P class="Body" style="margin: 0px"><BR>
97 <BR>
98 <span class="OBOS-Function-Def">uint16 FindClosestColor16(rgb_color col)</span></P>
99 <P class="Body" style="margin: 0px"><BR>
100 Returns a 16-bit approximation of the given 32-bit color. Alpha values are ignored.</P>
101 <P class="Body" style="margin: 0px">1) Create an opaque, 16-bit approximation of col using the following calculations:</P>
102 <P class="Body" style="margin: 0px"> r16=(31*col.red)/255</P>
103 <P class="Body" style="margin: 0px"> g16=(31*col.green)/255</P>
104 <P class="Body" style="margin: 0px"> b16=(31*col.blue)/255</P>
105 <P class="Body" style="margin: 0px">2) Assign it to a uint16 using the same code as in SetRGBColor16() and return it.</P>
106 <P class="Body" style="margin: 0px"><BR>
107 <BR>
108 <span class="OBOS-Function-Def">uint16 FindClosestColor15(rgb_color col)</span></P>
109 <P class="Body" style="margin: 0px"><BR>
110 This functions almost exactly like the 16-bit version, but this also takes into account the alpha transparency bit and works in the color space B_RGBA15. Follow the same algorithm as FindClosestColor16(), but assign the return value using SetRGBColor15.</P>
111 <P class="Body" style="margin: 0px"><BR>
112 <BR>
113 <span class="OBOS-Function-Def">rgb_color MakeBlendColor(rgb_color col, rgb_color col2, float position)</span></P>
114 <P class="Body" style="margin: 0px"><BR>
115 MakeBlendColor calculates a color that is somewhere between start color col and end color col2, based on position, where 0&lt;= position &lt;= 1. If position is out of these bounds, a color of {0,0,0,0} is returned. If position is 0, the start color is returned. If position is 1, col2 is returned. Otherwise, the color is calculated thus:</P>
116 <P class="Body" style="margin: 0px"><BR>
117 1) calculate delta values for each channel, i.e. int16 delta_r=col.red-col2.red</P>
118 <P class="Body" style="margin: 0px">2) Based on these delta values, calculate the blend values for each channel, i.e. blend_color.red=uint8(col1.red - (delta_r * position) )</P>
119 <DIV class="layer" id="Layer 1">
120 </DIV>
121 </DIV>
122 </BODY>
123 </HTML>