newbie : how to draw "semi-transparent" Rectangle

newbie : how to draw "semi-transparent" Rectangle

Post by TheBigBadWol » Sun, 29 Jun 2003 17:08:10



hi

Is it possible to draw a alpha-blended rectangle on a
TImage? I tried it with canvas.FillRect but that does seem
to work.
At the end it should look like the selection-rectangle in
WindowsXP-Explorer.

thanks in advance
wolf

 
 
 

newbie : how to draw "semi-transparent" Rectangle

Post by Ignacio Vazque » Sun, 29 Jun 2003 17:20:09



Quote:> Is it possible to draw a alpha-blended rectangle on a
> TImage? I tried it with canvas.FillRect but that does seem
> to work.

Use Windows.AlphaBlend.

Cheers,
  Ignacio

 
 
 

newbie : how to draw "semi-transparent" Rectangle

Post by Roy Magne Kleve » Sun, 29 Jun 2003 20:02:47


Hi,

I have done this but not using FillRect. I used scanlines and 32bit bitmaps
(for speed). The problem is the speed. On the pluss side it works in all
windows versions :)
It is a rather complex solution and I guess you need to understand how to
work with scanlines. I am using it in a thumbnail component it uses ImageEn
to load the files if you have ImageEn I could send you the component and you
could take a look at it your self. I could send it to you anyway but you
will not be able to compile it...

Kind regards
Roy Magne Klever
www.rmklever.com

 
 
 

newbie : how to draw "semi-transparent" Rectangle

Post by Wolfgang Lechne » Sun, 29 Jun 2003 23:19:43


Hi Roy
thanks for your response

Im working on a Chess-Tutorial - and I thought it would
be a cool idea to indicate for each field whether it is
protected by white or black. (e.g. black protection is
indicated with a red color, and white with green - a
field protect by two white pieces is "more green" than
a field protected by only one white pawn ... and so on)

My background is very simple (so speed should not be the
problem) - it is only 256 colors.
The color itself can be calculated before, so I only need
to draw 64 rectangles with knowen color but transparent
to see the board.

I have not worked alot with graphics, but I think this must
be a very easy task - maybe your component does **too much**
for this?

Arent transparent things done with XOR'S or AND'S or something
like this?

thanks
wolf



Quote:> Hi,

> I have done this but not using FillRect. I used scanlines and 32bit
bitmaps
> (for speed). The problem is the speed. On the pluss side it works in all
> windows versions :)
> It is a rather complex solution and I guess you need to understand how to
> work with scanlines. I am using it in a thumbnail component it uses
ImageEn
> to load the files if you have ImageEn I could send you the component and
you
> could take a look at it your self. I could send it to you anyway but you
> will not be able to compile it...

> Kind regards
> Roy Magne Klever
> www.rmklever.com

 
 
 

newbie : how to draw "semi-transparent" Rectangle

Post by Roy Magne Kleve » Sun, 29 Jun 2003 23:53:39


Hi Wolfgang,

You are right, it would be an overkill to do it my way in your example. May
I ask how you are drawing the chessboard? Or is it how to draw the
chessboard you want to know?

Kind regards
Roy Magne Klever
www.rmklever.com

 
 
 

newbie : how to draw "semi-transparent" Rectangle

Post by Jens Grusche » Mon, 30 Jun 2003 00:40:44


Quote:> The color itself can be calculated before, so I only need
> to draw 64 rectangles with knowen color but transparent
> to see the board.

In this case I would sugguest to precalculate all colors you use...

function MixColors(C1, C2: TColor; Percent: Integer): TColor;
begin
  // split red, green, blue channel and mix them separately,
  // then "or" them together again:
  Result := ((C1 and 255) + (C2 and 255)*Percent div 100)
  or (((C1 shr 8 and 255) + (C2 shr 8 and 255)*Percent div 100) shl 8)
  or (((C1 shr 16 and 255) + (C2 shr 16 and 255)*Percent div 100) shl 8);
end;

// MixColors(clRed, clBlue, 0) returns clRed
// MixColors(clRed, clBlue, 100) returns clBlue
// MixColors(clRed, clBlue, 50) returns some purple color

ChessColors: array[0..3, 0..10] of TColor;

for i := 0 to 10 do ChessColors[0, i] := MixColors(clBlack, clGreen, i*10);
for i := 0 to 10 do ChessColors[1, i] := MixColors(clBlack, clRed, i*10);
for i := 0 to 10 do ChessColors[2, i] := MixColors(clWhite, clGreen, i*10);
for i := 0 to 10 do ChessColors[3, i] := MixColors(clWhite, clRed, i*10);

Now simply draw each field of your board using the colors from ChessColors.

Quote:> Arent transparent things done with XOR'S or AND'S or something
> like this?

Yes and no :-)

You can erase parts of an image using AND (making them 0 / black), and set
other colors at these places without touching the rest unsing XOR.  Look at
following table:

Original    Mask     Overlay    Result
x               0             y              y
x               1             0               x (transparent)
x               1             y              strange result (only use this
if you know what you do)

So the mask tells what pixels you want to change (0) and what pixels you
want to keep (1).

As you see this only works for 0% or 100% transparency (but there is a trick
for 50%: simply apply 0% to one pixel and 100% to the next, like a
chessboard). If you really need values inbetween (looks much better), you
have to use multiplications instead of AND / XOR. This works only for
Highcolor / Truecolor, not for 256 colors or any other color mode with an
indexed palette (because the resulting colors might not be available). The
formula is similar to the one of MixColors above. But it's a bit more tricky
to make it fast (using ScanLine is a must).

There are a lot of tutorials out there, but maybe you want to use GDI+ or
Graphics32 (http://www.g32.org), which both are graphics libraries, that
make semi transparency easy (GDI+ is the Microsoft way and only works if you
have the GDI+ installed, Graphics32 is a freeware library and works on any
system).

Jens

 
 
 

newbie : how to draw "semi-transparent" Rectangle

Post by TheBigBadWol » Wed, 02 Jul 2003 04:03:50


thousand thanks for that, was a great help
dont think I need a graphic library for that, your
mixcolors function does the job!!!

thanks wolf


Quote:> > The color itself can be calculated before, so I only need
> > to draw 64 rectangles with knowen color but transparent
> > to see the board.

> In this case I would sugguest to precalculate all colors you use...

> function MixColors(C1, C2: TColor; Percent: Integer): TColor;
> begin
>   // split red, green, blue channel and mix them separately,
>   // then "or" them together again:
>   Result := ((C1 and 255) + (C2 and 255)*Percent div 100)
>   or (((C1 shr 8 and 255) + (C2 shr 8 and 255)*Percent div 100) shl 8)
>   or (((C1 shr 16 and 255) + (C2 shr 16 and 255)*Percent div 100) shl 8);
> end;

> // MixColors(clRed, clBlue, 0) returns clRed
> // MixColors(clRed, clBlue, 100) returns clBlue
> // MixColors(clRed, clBlue, 50) returns some purple color

> ChessColors: array[0..3, 0..10] of TColor;

> for i := 0 to 10 do ChessColors[0, i] := MixColors(clBlack, clGreen,
i*10);
> for i := 0 to 10 do ChessColors[1, i] := MixColors(clBlack, clRed, i*10);
> for i := 0 to 10 do ChessColors[2, i] := MixColors(clWhite, clGreen,
i*10);
> for i := 0 to 10 do ChessColors[3, i] := MixColors(clWhite, clRed, i*10);

> Now simply draw each field of your board using the colors from
ChessColors.

> > Arent transparent things done with XOR'S or AND'S or something
> > like this?

> Yes and no :-)

> You can erase parts of an image using AND (making them 0 / black), and set
> other colors at these places without touching the rest unsing XOR.  Look
at
> following table:

> Original    Mask     Overlay    Result
> x               0             y              y
> x               1             0               x (transparent)
> x               1             y              strange result (only use this
> if you know what you do)

> So the mask tells what pixels you want to change (0) and what pixels you
> want to keep (1).

> As you see this only works for 0% or 100% transparency (but there is a
trick
> for 50%: simply apply 0% to one pixel and 100% to the next, like a
> chessboard). If you really need values inbetween (looks much better), you
> have to use multiplications instead of AND / XOR. This works only for
> Highcolor / Truecolor, not for 256 colors or any other color mode with an
> indexed palette (because the resulting colors might not be available). The
> formula is similar to the one of MixColors above. But it's a bit more
tricky
> to make it fast (using ScanLine is a must).

> There are a lot of tutorials out there, but maybe you want to use GDI+ or
> Graphics32 (http://www.g32.org), which both are graphics libraries, that
> make semi transparency easy (GDI+ is the Microsoft way and only works if
you
> have the GDI+ installed, Graphics32 is a freeware library and works on any
> system).

> Jens

 
 
 

1. Newbie: Drawing with "Transparent"?

All,

I have a 64x64 pattern .pat image and it has a light green background
with some cartoonish trees on it. I wanted to make the trees more dense,
without having any selected copy/paste background trampling over and
erasing the original non-background parts.

So, I made a new transparent layer, used the rectangular select to
select a "tree" (and I got some of the background) and then I pasted
the tree selection into the new transparent layer.

Now I want to mark the few pixels that came over to the transparent layer
with the transparent color, so they wont be green anymore. That way, I can
copy and paste my no-background tree all I want onto various layers.

BUT, I cant find any option in the color selector to draw in the
"transparent" color.

How do I do that?

Lewis

2. miroVideo DC20 Plus

3. Newbie Question: How to draw "Pipes"??

4. Renderng to AVI files

5. ****"""""BUG IN PROGRAM"""*****

6. Labtec CS-1400 Speakers

7. FUTURA SEMI BOLD "HELP ME"

8. programming forums

9. Semi Transparent Rectangles

10. Semi-transparent filled selection rectangle?

11. Dimensions of "Golden Rectangle"??

12. Problem with "Transparent" Rectangles drawn over image

13. """"LOOKING FOR METRAEYES MODELLER AND TURBO.PXP FOR AN EXCHANGE"""""