JWM Source Documentation
gradient.c
Go to the documentation of this file.
1 
10 #include "jwm.h"
11 #include "gradient.h"
12 #include "color.h"
13 #include "main.h"
14 
16 void DrawHorizontalGradient(Drawable d, GC g,
17  long fromColor, long toColor,
18  int x, int y,
19  unsigned int width, unsigned int height)
20 {
21 
22  const int shift = 15;
23  unsigned int line;
24  XColor colors[2];
25  int red, green, blue;
26  int ared, agreen, ablue;
27  int bred, bgreen, bblue;
28  int redStep, greenStep, blueStep;
29 
30  /* Return if there's nothing to do. */
31  if(width == 0 || height == 0) {
32  return;
33  }
34 
35  /* Here we assume that the background was filled elsewhere. */
36  if(fromColor == toColor) {
37  return;
38  }
39 
40  /* Query the from/to colors. */
41  colors[0].pixel = fromColor;
42  colors[1].pixel = toColor;
43  JXQueryColors(display, rootColormap, colors, 2);
44 
45  /* Set the "from" color. */
46  ared = (unsigned int)colors[0].red << shift;
47  agreen = (unsigned int)colors[0].green << shift;
48  ablue = (unsigned int)colors[0].blue << shift;
49 
50  /* Set the "to" color. */
51  bred = (unsigned int)colors[1].red << shift;
52  bgreen = (unsigned int)colors[1].green << shift;
53  bblue = (unsigned int)colors[1].blue << shift;
54 
55  /* Determine the step. */
56  redStep = (bred - ared) / (int)height;
57  greenStep = (bgreen - agreen) / (int)height;
58  blueStep = (bblue - ablue) / (int)height;
59 
60  /* Loop over each line. */
61  red = ared;
62  blue = ablue;
63  green = agreen;
64  for(line = 0; line < height; line++) {
65 
66  /* Determine the color for this line. */
67  colors[0].red = (unsigned short)(red >> shift);
68  colors[0].green = (unsigned short)(green >> shift);
69  colors[0].blue = (unsigned short)(blue >> shift);
70 
71  GetColor(&colors[0]);
72 
73  /* Draw the line. */
74  JXSetForeground(display, g, colors[0].pixel);
75  JXDrawLine(display, d, g, x, y + line, x + width - 1, y + line);
76 
77  red += redStep;
78  green += greenStep;
79  blue += blueStep;
80 
81  }
82 }

joewing.net / Projects / JWM