andy's php projects
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Script and site usage:
All of the scripts on this site are provided free for non-commerical, personal or non-profit use. If you find these scripts useful to you, appreciate the free support, or are just an all round nice person, then why not donate a little dosh? If you wish to use any of these in a commercial system, please refer to the payment plan.


Change Hue of a greyscale image

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> Image Functions
View previous topic :: View next topic  
Author Message
freddo
Newbie


Joined: 19 Oct 2004
Posts: 3

PostPosted: Tue Oct 19, 2004 9:20 pm    Post subject: Change Hue of a greyscale image Reply with quote

I was wondering how to use this class to change the hue of a greyscale image. Basically this would give me a color wash of the image.

I should go into more detail:

Basically i want to allow a user to input a hex color into a form. the form will then create a table with the hex color border and I want the greyscale image to be washed in the hex color (sort of). I want to keep saturation at 90, which gives a nice washed out look and get the hue value from the hex color. Luminosity doesn't seem to matter.

I can see how to get the rgb values from your class, but i don't see how to get the hue value or apply it to the image.

Lets see:
$hex = '#45F2C3';
$hls = Image::rgb2hls(Image::hex2rgb($hex));
$hue = $hls[0];
$sat = $hls[1];
$lum = $hls[2];

This is where I get stuck. $sat will be a value between 0 and 1, I'm used to PSP which uses a value between 0 and 255. So I want about 35%.
So do i set $sat to 0.35 ???
$lum I guess i set to about the same (35%).

So maybe:
$hls[1]=0.35;
$hls[2]=0.35;
$new_hex = Image::rgb2hex(Image::hls2rgb($hls));

then (going out on a limb):
$img = imagecreatefromjpeg('original.jpg');
Image::colourise($img, $new_hex);
imagejpeg($img, 'processed.jpg', 90);

send headers blah blah
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Tue Oct 26, 2004 9:26 am    Post subject: Reply with quote

Hi Freddo, and welcome to the board!

Sorry it took a little while to respond to this post, but on reading it again there are some questions...

When you say yo uwant to give a 'washed out' colour at a certain percentage, is it the colour you want to give a percentage of, or actual luminensence? For example, if I gave pure red as the hex colour, do you actually want the colourise be be 35% of pure red? Or is it a case that you want to saturate the image to 35% it's strength (basically apply a white wash on it) and then colourise at full strength? Does that make sense?

Incidently, you don't need to greyscale before you colourise. It's a step (and a whole chunk of processing and time) you can avoid.

Oh, and this is wrong:

Code:
$hue = $hls[0];
$sat = $hls[1];
$lum = $hls[2];


hue = $hls[0], lum = $hls[1] and sat = $hls[2] (h.l.s., in that order).

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
freddo
Newbie


Joined: 19 Oct 2004
Posts: 3

PostPosted: Tue Oct 26, 2004 3:41 pm    Post subject: Reply with quote

Hi Andy,

Firstly I should say I'm not an expert at graphics. So it's not surprising I've confused you.

I'm starting with a greyscale image. I have a page where someone can change the colour of the borders, by choosing a hex colour. I've used a greyscale image because grey works with all 16 million hex colours (if i did the image in blue it might clash if someone wanted pink).

What i thought would be cool, would be if I could change the image from greyscale to a colour that matched the borders. Now if someone picked a bright red, i don't want the image bright red but a washed out red.

Playing with Paint Shop Pro i can do that to the greyscale image by making the lum and sat set at 90 (scale of 1 to 255 - 30%) and then just moving the Hue slider. I get nice washed out colours on the image. Just wondering if your class can do this? and how? Thought you might know off the top of your head. When I get a bit of time i might play with the class and see if I can make it happen....
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Wed Oct 27, 2004 4:05 am    Post subject: Reply with quote

The easy answer is; yes, the class can do this, with a little tweaking.

You see, all you need to do is really tweak the colourise function to alter the hue, luminesence and saturation.

Look at this code:

PHP:
<?php function colourise(&$im$setColour)
    {
        if (
is_string($setColour)) {
            
$hls Image::rgb2hls(Image::hex2rgb($setColour));
        } else {
            
$hls Image::rgb2hls($setColour);
        }
        
        
$sx imagesx($im);
        
$sy imagesy($im);
        for (
$x 0$x $sx$x++) {
            for (
$y 0$y $sy$y++) {
                
$rgb Image::getPixelRGB($im$x$y);
                
$pxhls Image::rgb2hls($rgb);
                
// determines the actual colour
                
$pxhls[0] = $hls[0];
                
// makes more of the image whiter
                
$pxhls[1] = ($pxhls[1] + $hls[1] > 1) ? $pxhls[1] + $hls[1];
                
// controls how much of the $setColour is shown
                
$pxhls[2] = 0.2;
                
$rgb Image::hls2rgb($pxhls);
                
imagesetpixel($im$x$yimagecolorallocate($im$rgb[0], $rgb[1], $rgb[2]));
            }
        } 
    } 
?>


You can see in there I have updated the $pxhls assignment. Let's look at the lines:

PHP:
<?php // determines the actual colour
                
$pxhls[0] = $hls[0];
                
// makes more of the image whiter
                
$pxhls[1] = ($pxhls[1] + $hls[1] > 1) ? $pxhls[1] + $hls[1];
                
// controls how much of the $setColour is shown
                
$pxhls[2] = 0.2?>


The hue gets set to whatever colour I pass in. Imagine in this instance I set the colour to be 'FF0000' - red.

For the hell of it I'm bumping up the luminescence to make the image look bleached out. I'm doing this in a simple way of just adding the lum value of them both together. Because the range is 0-1, if the value is about 1 I set it to 1. If you wanted to saturate the image with white to a certain percentage, then you'd add the percentage value (0.35, in your case) to the $pxhls and check it doesn't go above 1. If you wanted to keep the lum value the same then simply remove this line which will keep $pxhls[1] the same.

Then I set the saturation of the colour to 0.2. I'm doing this just to give a slight colour effect of the red. You could try the 0.35 value here to get that saturation level.

If you update the colourise function then it's simply a case of calling it like this:

PHP:
<?php

    
require_once('class.image.php');

    
set_time_limit(120);
    
$img imagecreatefromjpeg('original.jpg');
    
Image::colourise($img'FF0000');

    
header('Content-type: image/jpeg');
    
imagejpeg($img);

?>


You could always update the colourise function to take an additional parameter of 'percetange'.

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
freddo
Newbie


Joined: 19 Oct 2004
Posts: 3

PostPosted: Thu Oct 28, 2004 12:45 am    Post subject: Reply with quote

Thanks for that, when i have time I'll go over this and post back how i go.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> Image Functions All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group