/    Sign up×
Community /Pin to ProfileBookmark

change color in png

If I have an png image like the attachment, is it possible to change the color of it with the help of the gd library.

For example would it be possible to provide an RGB color and then change the image to the desired color?

[upl-file uuid=ef1785c7-1ccd-429f-9c37-34043f77bfba size=702B]gradient.png[/upl-file]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@bokehSep 18.2006 — What do you mean change the colour? Obviously it is already some number of different colours already. Do you mean shift the hue?
Copy linkTweet thisAlerts:
@stephan_gerlachauthorSep 18.2006 — Not sure what the hue is but basically I want to make it so that the blue gradient can be changed to green, red, orange, yellow, purple ect ect.
Copy linkTweet thisAlerts:
@bokehSep 18.2006 — [URL=http://en.wikipedia.org/wiki/Hue]Hue[/URL]

Run through the image pixel by pixel. Convert the RGB values to luminence and chroma. Shift the chroma around the colour wheel the desired number of degrees and then recombine that value with the original luminence value and convert back to an RGB and set the pixel to that value.

If you want to shift to a colour rather than an angle you would need to run through the image and get the average chroma, then add or subtract that from the desired chroma to get the shift angle and then run through each pixel shifting each one by that amount.
Copy linkTweet thisAlerts:
@bokehSep 18.2006 — [URL=http://www.easyrgb.com/math.php]Here is a site[/URL] with the formulas you need to do the conversion. It looks pretty simple to me but I don't have time to investigate it right now. If you still haven't figured it out by tomorrow I'll have a look.
Copy linkTweet thisAlerts:
@stephan_gerlachauthorSep 18.2006 — cool. thanks alot.
Copy linkTweet thisAlerts:
@bokehSep 18.2006 — Can you describe the overall effect of the whole image project in detail? This will allow all parts of the project to be tied together in one in one class rather than attacking them piecemeal as has been done thus far. Also can you zip up all the images together and attach them to this thread?
Copy linkTweet thisAlerts:
@stephan_gerlachauthorSep 18.2006 — Unforunatly at the moment I can not give more details.

I am building a class together. I just haven't used the gd library before. Thats why I need the help.

This is the last problem I need to solve in order to code my entire project.

Once it is finished I will post the url here
Copy linkTweet thisAlerts:
@stephan_gerlachauthorSep 18.2006 — On this page

[URL=http://uk2.php.net/manual/en/function.imagefilter.php]http://uk2.php.net/manual/en/function.imagefilter.php[/URL]

I found the following code

[code=php]

<?php
$im = imagecreatefrompng('philip.png');

/* R, G, B, so 0, 255, 0 is green */
if ($im && imagefilter($im, IMG_FILTER_COLORIZE, 0, 255, 0)) {
echo 'Image successfully shaded green.';
imagepng($im, 'philip.png');
} else {
echo 'Green shading failed.';
}

imagedestroy($im);
?> [/code]


this makes it possible quite easily to change the color, BUT the color does not seem to be the one i selected.

Can this be cause by the fact that that the starting color is blue??
Copy linkTweet thisAlerts:
@bokehSep 18.2006 — Blimey! What a sickening effect!
Copy linkTweet thisAlerts:
@bokehSep 18.2006 — Hmm! This hasn't turned out quite how I expected!

[upl-file uuid=f941845c-9b24-4f93-b68a-6b73731727fd size=13kB]original.jpg[/upl-file]

[upl-file uuid=80c02e9f-8569-449e-a8ad-3bc8ba3659c2 size=13kB]red_mask.jpg[/upl-file]

[upl-file uuid=81b434e4-a77d-4620-8de1-34462cd2ec0a size=13kB]green_mask.jpg[/upl-file]

[upl-file uuid=47cc0bfb-f560-4f04-9aa3-0c9461160bab size=13kB]blue_mask.jpg[/upl-file]
×

Success!

Help @stephan_gerlach spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...