Resolved Image ColorKey and ColorMatrix

Calix

New member
Joined
Aug 20, 2010
Messages
1
Programming Experience
Beginner
Hello everyone, first post here and looking for some guidance on what I think should be something simple, but I've been playing with it for hours getting nowhere.

Perhaps I don't understand the ColorMatrix well enough, but there is almost no documentation on it, well anywhere. What I am trying to do is, using GDI, I want to draw part of an image from a bigger image (a specific tile from a bigger tileset if you will), onto a new image. This image is then drawn to a picturebox which represents a map. If it makes a difference, I am not using DirectX or XNA for this because it is a very simple and quick mapeditor and not the actual game client.

In addition, when placing a tile from the tileset onto a map, the mapeditor allows a color tint including the alpha channel, and a general overall opacity. This is all working well together using a colormatrix. The problem I am having is that the tileset has a color that specifies that it should be drawn transparent. I tried using a ColorKey and it works, as long as I don't use the ColorMatrix. However, I need the color matrix for the color tinting. Is there a way to use them together, or perhaps implement the ColorKey into the ColorMatrix?

Here is the drawing routine so far:
VB.NET:
Private Sub PaintTile(ByVal p As Point)
	MapGraphics.FillRectangle(Brushes.Black, p.X * TileWidth, p.Y * TileHeight, TileWidth, TileHeight)
	If CurMap.Tile(p.X, p.Y).ImageIndex <> -1 Then
		Dim img As Bitmap = GetTileFromTileSet(CurMap.Tile(p.X, p.Y).ImageIndex)
		Dim imgAttr As New Imaging.ImageAttributes()
		Dim tilecolor() As Single = _
			{
				CurMap.Tile(p.X, p.Y).Tint.R / 255, _
				CurMap.Tile(p.X, p.Y).Tint.G / 255, _
				CurMap.Tile(p.X, p.Y).Tint.B / 255, _
				CurMap.Tile(p.X, p.Y).Tint.A / 255
			}

		imgAttr.SetColorKey(Color.Magenta, Color.Magenta)
		If tilecolor(0) <> 255 Or tilecolor(1) <> 255 Or tilecolor(2) <> 255 Then
			imgAttr.SetColorMatrix( _
					New Imaging.ColorMatrix( _
						{
							New Single() {1.0F, 0.0F, 0.0F, 0.0F, 0.0F}, _
							New Single() {0.0F, 1.0F, 0.0F, 0.0F, 0.0F}, _
							New Single() {0.0F, 0.0F, 1.0F, 0.0F, 0.0F}, _
							New Single() {0.0F, 0.0F, 0.0F, CurMap.Tile(p.X, p.Y).Opacity, 0.0F}, _
							New Single() {tilecolor(0), tilecolor(1), tilecolor(2), tilecolor(3), 1.0F}
						}
					), Imaging.ColorMatrixFlag.Default, Imaging.ColorAdjustType.Bitmap)
		Else
			imgAttr.SetColorMatrix( _
					New Imaging.ColorMatrix( _
						{
							New Single() {1.0F, 0.0F, 0.0F, 0.0F, 0.0F}, _
							New Single() {0.0F, 1.0F, 0.0F, 0.0F, 0.0F}, _
							New Single() {0.0F, 0.0F, 1.0F, 0.0F, 0.0F}, _
							New Single() {0.0F, 0.0F, 0.0F, CurMap.Tile(p.X, p.Y).Opacity, 0.0F}, _
							New Single() {0.0F, 0.0F, 0.0F, 0.0F, 1.0F}
						}
					), Imaging.ColorMatrixFlag.Default, Imaging.ColorAdjustType.Bitmap)
		End If
		CurMapGraphics.DrawImage(img, New Rectangle(p.X * TileWidth, p.Y * TileHeight, TileWidth, TileHeight), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttr)
	End If
End Sub

Please ignore obvious code errors in the above code, I simplified some parts of the code to be easier to understand without having to post the entire sourcecode.

Thank you in advance for the help.

[EDIT]
Well I spent a little more time working on it and gave up and used an easy little hack to fix it. Instead of using both the ColorKey and the ColorMatrix, I first create a temporary image that uses the colorkey, then use the temporary image to draw to the mapgraphics. Not pretty, but it works.
 
Last edited:
Back
Top