0

I have a bitmap palette where i can easily change a single color for the whole palette or all of them by inverting them all for example:

for (int i = 0; i < sprite.Palette.Length; i++)
{
    int r = Byte.MaxValue - sprite.Palette[i].R;
    int g = Byte.MaxValue - sprite.Palette[i].G;
    int b = Byte.MaxValue - sprite.Palette[i].B;    

    sprite.Palette[i] = Color.FromArgb(r, g, b);  
}

But i don't want to change all the colors. I just want to change certain pixels in the palette. If it was a Bitmap i could just do this:

sprite[i].Bitmap.SetPixel(x, y, Color.FromArgb(0, 0, 255));

Which would just change those pixels at x & y, but obviously that doesn't work for a palette.

Edit: would need to convert the bitmap back to a byte array, but seeing as it is a sprite the usual methods to converting bitmap to byte array don't work..

  • _"If it was a Bitmap"_ - what is it then? You can use [this library](https://github.com/koszeggy/KGySoft.Drawing#managed-bitmap-data-manipulation) (disclaimer: written by me) to treat any managed or unmanaged buffer as a bitmap. Eg. `var myBitmapData = BitmapDataFactory.CreateBitmapData(buf, size, stride, KnownPixelFormat.Format8bppIndexed, palette)`, and then you can simply use `myBitmapData.SetPixel(x, y, color)`, which works even for indexed formats. – György Kőszeg Jul 24 '22 at 11:28
  • Please tell us about that Palette class/object you have. Where does it come from? The Palette of a palette-based image can only be replaced as a whole after creating it with the colors you want, but I guess yours is a different class..? - You don't really ask how to replace a loop by a single assignment, I'd guess.. – TaW Jul 24 '22 at 12:07
  • It's a sprite byte[]. I can modify it by setting the palette and can access it as a bitmap, but modifying the bitmap does nothing as the bitmap appears to just be for visualising the palette changes as you make them. So i guess i need to set the pixels on data array or palette somehow? – C0deM0nkey Jul 24 '22 at 12:18
  • Here is where i got the [source code](https://mega.nz/file/pwY2CSaS#Ci18weivXGcyIcXyVHe8hAkGFEcDgtFdZNDz0svULzA). I am using the Sprite.cs file to load a sprite (.spl file) @GyörgyKőszeg – C0deM0nkey Jul 24 '22 at 14:34
  • 1
    As I see it handles everything at low level and even converts the byte array to a `Bitmap` in `SplFrameToBmp`. So even if you change the buffer it will not change the already generated bitmaps until you call `SplFrameToBmp` again. Which btw creates a 24bpp image from the indexed data, which will be turned into a 32bpp one when calling `MakeTransparent`. – György Kőszeg Jul 24 '22 at 18:43
  • Yeah, it is possible to convert a modded bitmap back to a byte array as others have done it before, but that was some time ago and they have not shared the source code for that part. Do you have any idea how it can be done? @GyörgyKőszeg – C0deM0nkey Jul 24 '22 at 20:40
  • @C0deM0nkey: If you want to change the pixels, not just the palette colors, it means you want to create a different sprite. _"Do you have any idea"_ - yes but maybe it's not relevant as I would not use that sprite implementation if I had to create sprites. Instead, I would use my `BitmapDataFactory` to create the bitmaps of the sprites, so I could set the pixels by colors (even for indexed sprites with a palette), and eventually I would call `ToBitmap` when I need an actual `Bitmap` that represents my sprite. – György Kőszeg Jul 25 '22 at 15:36
  • Don't know if solved but regarding _"I don't want to change all the colors. I just want to change certain pixels in the palette"_ Well what is the palette length? Can you not try as `sprite.Palette[i].R = someValue;` and also do same for the Green and Blue channels? Where the `[i]` is the position of your "to change" colour within the palette array... – VC.One Aug 07 '22 at 17:34

0 Answers0