[plt-scheme] The transparent color?

From: Ray Racine (rracine at adelphia.net)
Date: Tue Dec 3 20:39:40 EST 2002

I had the same issue.  I was using a prexisting XPM file.
And was unable leverage the GIF mask support.  
So I created a mask from the pixmap so only the "unit"
is displayed by masking off the background.

;;====================================================
;; Create a mono mask by iterating through all pixels
;; of a pixmap.  
;; Black (the alpha color) is white in the mask.
;; All other colors map to black.
;;====================================================
 (define (pixmap->bitmap pixmap)
    (let* ((white (make-object color% "white"))
           (black (make-object color% "black"))
           (cols (send pixmap get-width))
           (rows (send pixmap get-height))
           (pixmap-dc (make-object bitmap-dc%))
           (bitmap (make-object bitmap% cols rows #t))
           (bitmap-dc (make-object bitmap-dc%))
           (pix-color (make-object color% 0 0 0)))
      (send bitmap-dc set-bitmap bitmap)
      (send pixmap-dc set-bitmap pixmap)
      (do ((r 0 (add1 r)))
        ((= r rows) (void))
        (do ((c 0 (add1 c)))
          ((= c cols) (void))
          (begin
            (send pixmap-dc get-pixel c r pix-color)
            (if (color-eq? pix-color black)
                (send bitmap-dc set-pixel c r white)
                (send bitmap-dc set-pixel c r black)))))
      (send bitmap-dc set-bitmap #f)
      (send pixmap-dc set-bitmap #f)
      bitmap))

then ....

;; save the mask for later
 (send tile set-loaded-mask (pixmap->bitmap tile))

later....

;; draw the unit masking the background away.
(send dc draw-bitmap unit-pixmap
                     x
                     y	
                     'solid black
                     (send unit-pixmap get-loaded-mask))

Any enhancements to the drawing API would be great.
For example.
1. a more efficient pixmap to monochrome call.
2. mask support for XPMs.

But so far I can do everything I need to do with what this is there.




On Tue, 2002-12-03 at 18:26, Matthew Flatt wrote:
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> At Thu, 28 Nov 2002 17:45:12 -0500, "Ghis Lafortune" wrote:
> > I'm a student of Sherbrook's University and I'm programming a chess game. I've 
> > encounter a problem with the pieces. I've put a transparent background but 
> > Drscheme show it white. 
> > I'm using a viewport and all my pics are in gif format. 
> > How do I say to Drscheme to know that the "white" background is transparent? 
> 
> Short answer:
> 
> The viewport graphics library doesn't currently draw bitmaps with
> transparency. I've fixed that for the next release.
> 
> 
> Slightly longer answer:
> 
> By using the underlying MrEd toolbox, It's almost possible to draw GIFs
> with transparent backgrounds. There is a bug, however, in the v202
> distribution.
> 
> (Also, the `viewport-dc' and `viewport-buffer-dc' functions, which
> provide a bridge to the underlying toolbox, seem to be undocumented.
> I've fixed that for the next release, too.)
> 
> 
> Long answer:
> 
> In version 202, you can use the type 'gif/mask (or 'unknown/mask) when
> loading a bitmap from a file. If the file contains a GIF with a
> transparent index, the resulting bitmap will contain a mask bitmap,
> which you can access through the `get-loaded-mask' method. Finally, use
> the mask bitmap as the last argument to dc<%>'s `draw-bitmap' when
> drawing the loaded bitmap. (In future versions, maybe the mask bitmap
> stored with the main bitmap will be used automatically.)
> 
> At least, that's the theory. But the `get-loaded-mask' method was
> missing in the v202 distribution. You can correct the bug by editing
>    plt/collects/mred/private/kernel.ss
> Find the definition of bitmap%, and add `get-loaded-mask' to its list
> of methods.
> 
> 
> Matthew
-- 
Ray Racine <rracine at adelphia.net>



Posted on the users mailing list.