0

I am designing a Mandlebrot Fractal in C# which is working fine... Question is I want to cycle through all the rgb colours in effect what I think is called colour cycling... I know that I would need to convert the bitmap to a pallet and hence run through each pixel???? How would I achieve this?? Working on this for hours now and really unsure.

Thanks

Ibrahiem Rafiq
  • 73
  • 2
  • 6
  • 16

2 Answers2

3

Using HSV instead of RGB would probably make cycling the colors a lot easier, since you just increment (and modulo) a single value instead of trying to manage 3 of them.

Pyritie
  • 543
  • 4
  • 16
3

Agree with @Pyritie, use HSV rather than RGB. See this question for examples.

Then try this code to set palette colours of a bitmap:

var bitmap = new Bitmap(width, height, width, PixelFormat.Format8bppIndexed);

ColorPalette palette = bitmap.Palette;
palette.Entries[0] = Color.Black;
for (int i = 1; i < 256; i++)
{
    // set to whatever colour here...
    palette.Entries[i] = Color.FromArgb((i * 7) % 256, (i * 7) % 256, 255);
}
bitmap.Palette = palette;

Original credit belongs to Jon Skeet, from a TPL demo of his, I think.

Community
  • 1
  • 1
g t
  • 7,287
  • 7
  • 50
  • 85
  • 1
    The "bitmap.Palette =" is critical. You seemingly can't make changes to the palette and get them to stick without that set operation. Not intuitive on MSFT's part, so thanks for the code sample. – Marc Stober Mar 08 '13 at 21:47