Color

Now let's add color. If you don't have a color machine, this will still work. Of course, you won't be able to see the colors, so if you can find a color display to run this example on, that would be best.

     | ?- alloc_color(goldenrod, Pixel1),
          alloc_color(forestgreen, Pixel2),
          alloc_color(cyan, Pixel3),
          alloc_color(black, Pixel4),
          retractall(colors(_,_,_,_)),
          assert(colors(Pixel1,Pixel2,Pixel3,Pixel4)).
     
     Pixel1 = 6,
     Pixel2 = 7,
     Pixel3 = 8,
     Pixel4 = 1
     

Each line here allocates a color in the default colormap of the default screen. When you give alloc_color/2 a valid color, it always returns a pixel value, even for black and white screens. If it can't allocate the color you ask for, it will give you the closest one it can. In this case, we have chosen the colors so that on a monochrome system the two background colors (Pixel1 and Pixel2) will be different, and likewise the two foreground colors (Pixel3 and Pixel4), so that the window will look reasonable. It wouldn't do if all the colors were the same.

If you're following along typing in these examples, you probably didn't get the same pixel values as we did here. That's why we assert the values: so we can get the right pixel values later when we need them.

Now let's construct a new background that uses these colors.

     | ?- colors(Pixel1, Pixel2, _, _),
          create_pixmap(Pix, [size(4,4)]),
          put_graphics_attributes(Pix, [foreground(Pixel1)]),
          fill_rectangle(Pix, 0, 0, 3, 3),
          put_graphics_attributes(Pix, [foreground(Pixel2)]),
          draw_segments(Pix, [segment(0,0,3,3), segment(0,3,3,0)]),
          win(Window),
          put_window_attributes(Window,[background(Pix)]).
     
     Pixel1 = 6,
     Pixel2 = 7,
     Pix = pixmap(586904)
     

The only thing here that's new is the call to fill_rectangle/5. We call it here to fill the pixmap with the appropriate background color. The rest of this has been discussed before in pxl-tut-dbg.

Finally, let's put the message back, only in color.

     | ?- win(Window),
          colors(_, _, Pixel3, Pixel4),
          put_graphics_attributes(Window, [foreground(Pixel4)]),
          draw_string(Window, 6, 24, 'Hello, world!!'),
          put_graphics_attributes(Window, [foreground(Pixel3)]),
          draw_string(Window, 2, 19, 'Hello, world!!').
     
     Window = window(2376568),
     Pixel3 = 8,
     Pixel4 = 1,
     

This is exactly what we did before, only now we specify foreground/1 for the message, and the shadow, to take on the newly allocated colors.