# Code problem - Listing colors in image

## Recommended Posts

43 colors is a Cakewalk , 16 million colors is a quagmire.

If you do feel the need to process more than 43 colors heres a simple solution:

Since random is psuedo random. Simply Seed the Random generator with the color in question and all the ROI's are in sync.

I replace the reseed button (0 - 255) with a 0 - 32767 for more variations.

This will give you the speed of Multithreading and the palette consistency

```#region UICode
int Amount1 = 25; // [0,100] Hue Adjust
int Amount2 = 10; // [0,100] Saturation Adjust
int Amount3 = 10; // [0,100] Value Adjust
int Amount4 = 3; // [0,32767] Reseed
#endregion

object sync = new object();

void Render(Surface dst, Surface src, Rectangle rect)
{

bool match = false;
int colorIdx = 0;
Color[] colors = new Color[rect.Width * rect.Height];
Color[] off_colors = new Color[rect.Width * rect.Height];
ColorBgra CurrentPixel;

//populate colors
for (int y = rect.Top; y < rect.Bottom; y++)
{
for (int x = rect.Left; x < rect.Right; x++)
{
CurrentPixel = src[x,y];
bool nomatch=true;
for (int i = 0; i < colorIdx; i++)
{
if (CurrentPixel.ToColor().Equals(colors[i])){
nomatch=false;
dst[x,y]= ColorBgra.FromColor(off_colors[i]);
break;
}

}
if (nomatch){

colors[colorIdx] = CurrentPixel.ToColor();
Random rndm = new Random(CurrentPixel.GetHashCode()+ Amount4);
HsvColor hsv;
int H = 0, S = 0, V = 0;

hsv = HsvColor.FromColor(colors[colorIdx]);

H = hsv.Hue;
S = hsv.Saturation;
V = hsv.Value;

H -= rndm.Next(Amount1 * 180 / 100);
H += rndm.Next(Amount1 * 180 / 100);

S -= rndm.Next(Amount2) / 2;
S += rndm.Next(Amount2) / 2;

V -= rndm.Next(Amount3) / 2;
V += rndm.Next(Amount3) / 2;

if (H < 0) H = 0;
if (H > 360) H = 360;

if (S < 0) S = 0;
if (S > 100) S = 100;

if (V < 0) V = 0;
if (V > 100) V = 100;

hsv = new HsvColor(H,S,V);

off_colors[colorIdx]=hsv.ToColor();
dst[x,y]= ColorBgra.FromColor(off_colors[colorIdx]);

colorIdx++;
}
}
}
}
```
Edited by TechnoRobbo

Go out there and be amazing. Have Fun, TR

Some Pretty Pictures Some Cool Plugins

##### Share on other sites

Since random is Psuedo Random Simply Seed the Random generator with the color in question and all the ROI's are in sync.

Very clever!

I see you got it to respect selections too!

I still don't know what you use

```object sync = new object();
```

for in the code though.

I also like the reseed button but I can probably figure that out. I thought I posted this but I guess I closed the browser before posting.

##### Share on other sites

I still don't know what you use

object sync = new object();

for in the code though.

I forgot to erase it

I also like the reseed button but I can probably figure that out. I thought I posted this but I guess I closed the browser before posting.

reseed button cycles from 0 to 255 as you click on it- it will work too but the sliders will give you more combination (0 - 32767)

Post Edit:

I was just playing around with it -Kudos man, this is a cool concept!!!

I'm removing it from my machine and I will wait for your plugin.

Edited by TechnoRobbo

Go out there and be amazing. Have Fun, TR

Some Pretty Pictures Some Cool Plugins

##### Share on other sites

I'll have to see how others have posted plugins but I should have it out within a few days when I have more free time.

I very much appreciate the help

##### Share on other sites

In paint.net 4.0 you can also make use of PaintDotNet.RandomUtil.ThreadInstance. It's already initialized with a good seed which is thread- and time-dependent. You also won't end up with 2 threads using the same seed, which is important.

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×