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.


Bug in rgb2hls()?

 
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
TheOwl
Newbie


Joined: 09 Feb 2005
Posts: 2

PostPosted: Wed Feb 09, 2005 5:33 am    Post subject: Bug in rgb2hls()? Reply with quote

Hi there,

I needed HLStoRGB and RGBtoHLS conversion functions for a C program, and used ruthlessly the book Computer Graphics: Principles and Practice for reference (okay, I simply typed manually the functions provided there-in). However, these functions didn't work as expected; the RGBtoHLS function worked as it should, but the HLStoRGB didn't.

Enter Google.

I found your site through it, and studied your php-implementations of said functions. Nearly as in the book, but with minor changes. By changing my C-function HLStoRGB to be like your PHP-version, I magically fixed my program. I then changed my RGBtoHLS function to be like your PHP-implementation, but alas, that flipped the numbers quite oddly.

So, when I took the RGBtoHLS from the book and the HLStoRGB from your site, I got everything working. I believe there is a bug in your RGBtoHLS function.

In rgb2hls(), I'd change this line
Code:
$hls[2] = ($hls[1] <= 0.5) ? ($delta / ($max + $min)) : ($delta / (2 - $delta));

to this
Code:
$hls[2] = ($hls[1] <= 0.5) ? ($delta / ($max + $min)) : ($delta / (2 - ($max + $min)));


If this, however, isn't the proper way, I apologise. Although I am pretty certain that there is a bug in your PHP code, and I did run all the possible values of rgb thru RGBtoHLS and back with HLStoRGB to make sure that both work correctly (with the code changed as suggested), I am still just another human. Smile

Hope it helps.

Regards,
TheOwl

Edit: Yet again I wrote all HLS' as HSL. Damn HSV, get out of my head.
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 Feb 09, 2005 7:23 am    Post subject: Re: Bug in rgb2hls()? Reply with quote

Hi there, and welcome to the board!

It's a very informative book, isn't it? The best resource I could find for the colour space conversions (and as you noticed, mine come directly from that book, too).

I do believe you are right in that it should have ($max + $min), not ($max - $min) (or $delta) as I now have it. I'll have a play around with those tonight and see what gives.

Many thanks for pointing it out! Smile

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


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

PostPosted: Wed Feb 09, 2005 3:29 pm    Post subject: Reply with quote

Hi there,

So I just tested the function, and low-and-behold, it was producing dodge results. Can't believe I didn't notce that! Anyway, thanks for pointing it out, TheOwl!

The line should look like:

Code:
$hls[2] = ($hls[1] <= 0.5) ? ($delta / ($max + $min)) : ($delta / (2 - ($max + $min)));


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


Joined: 09 Feb 2005
Posts: 2

PostPosted: Thu Feb 10, 2005 11:35 pm    Post subject: Reply with quote

No problem! Glad to be of help; your code did help me too, so I thought it'd be only polite to point out that bug I found.

Regards,
TheOwl
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