Click here to Skip to main content
14,173,121 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I have a function which should change the colors in an image to their opposites. Red to green, green to blue, and blue to red. I have thinking and trying for a while now, but I can not get it to work. However, I know that there is not any problem with my code. Because the image does change colors, but I have a hard time to make it right.

What I have tried:

This is the code I have for now. I tried to look at a color wheel to see if I was able to move the pixel's color a third of the wheel. Color Wheel Chart Online[^]

My code looks like this:
void reColor(Image *img) {

	img->height = 200;
	img->width = 150;

	for (unsigned int i = 0; i < img->height; i++)
	{
		for (unsigned int j = 0; j < img->width; j++)
		{
			if ((img->pixels[i][j].r <= 255 && img->pixels[i][j].g <= 255)                        && img->pixels[i][j].b == 0){
				while (img->pixels[i][j].r != 0) {
					img->pixels[i][j].r -= 1;
					img->pixels[i][j].g += 1;
				}
			}
			else if ((img->pixels[i][j].r == 0 && img->pixels[i][j].g 
<= 255) && img->pixels[i][j].b <= 255) {
				while (img->pixels[i][j].g != 0) {
					img->pixels[i][j].g -= 1;
					img->pixels[i][j].b += 1;
				}
			}
			else if ((img->pixels[i][j].r <= 255 && img->pixels[i][j].g == 0) && img->pixels[i][j].b <= 255) {
				while (img->pixels[i][j].b != 0) {
					img->pixels[i][j].b -= 1;
					img->pixels[i][j].r += 1;
				}
			}
		}
	}
}
Posted
Updated 5-Jan-17 15:39pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

They seemed contradictory:
Quote:
change RGB colors to their opposites
Quote:
look at a color wheel to see if I was able to move the pixel's color a third of the wheel

It the title is correct, then isn't it just a matter of:
newR = 255 - oldR
newG = 255 - oldG
newB = 255 - oldB

regardless of the programming language.
   
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

A quick search of CodeProject in less than 30 seconds served up this gem: Fundamentals of Image Processing - behind the scenes[^]
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

If you want the inverted color which affects both hue and lightness, a simple invert will do.

Just convert from hex to RGB and invert each channel,

R = R *-1 + 255
G = G *-1 + 255
B = B *-1 + 255


And then convert back to hex.

If you want the opposite hue, without affecting lightness, the most straightforward way is to convert from hex to HSL and rotate the hue 180 degrees.

Convert from hex to HSL and rotate H 180 degrees.

H = H +180 % 360

And then convert back to hex.

Taken from here[^] .
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190524.3 | Last Updated 5 Jan 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100