Active Forums => QB64 Discussion => Topic started by: johannhowitzer on February 12, 2020, 05:50:12 pm
Title: Problem with HSL to RGB function
Post by: johannhowitzer on February 12, 2020, 05:50:12 pm
I've plugged the HSL to RGB colorspace conversion formula into a function, and tested it by drawing a strip from hue 0 upwards, staying at full saturation and half lightness. Instead of a smooth rainbow, I got six blocks of color, so it seems there's rounding going on somewhere and I can't find it.
I'm using HSL as values from 0 to 240, like mspaint's color picker does, but this is arbitrary and could easily be changed to work on some other scale by changing the value of v.
Title: Re: Problem with HSL to RGB function
Post by: FellippeHeitor on February 12, 2020, 07:09:27 pm
This one adapted from http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors (http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors) also takes an alpha parameter (values range from 0 to 360 - for hue - and from 0 to 255 for saturation, brightness and alpha):
[ This attachment cannot be displayed inline in 'Print Page' view ]
Title: Re: Problem with HSL to RGB function
Post by: RhoSigma on February 13, 2020, 02:27:27 am
In alternative you may try my converthelper.bm library, it's in the IMG-Support folder of my QB64 Library collection which you can download from the Bonus Stuff Section here: https://www.qb64.org/forum/index.php?topic=809.msg100182#msg100182
This library contains raw converting routines between RGB/HSB and vise versa, but also some convenience FUNCTIONs, which are in symmetry with the respective RGB colorspace functions provided by QB64 itself:
Documentation and examples are also provided in the archive.
EDIT: Make sure to move the extracted QB64Library folder with its entire contents into your QB64 folder (where qb64.exe is in). You find an overview of all libraries in the QB64Library-Info.html file.
Title: Re: Problem with HSL to RGB function
Post by: johannhowitzer on February 14, 2020, 11:50:45 pm
Those are cool and helpful and all, and thanks... but if you don't mind, it's clear I made a mistake somewhere in here, and if possible I'd like to figure out what it is. I'll take another comb through it when I get a chance, and try to compare to your function when I can. Speaking of which, why's it bleeding past red back into yellow and then red and then yellow again?
Title: Re: Problem with HSL to RGB function
Post by: FellippeHeitor on February 14, 2020, 11:55:35 pm
I took the screenshot before I fixed the code to post. It was still taking 255 for hue and I did the loop up to 360. The version of the code I posted will produce a slightly different output.
Also I do apologize: it is a bad habit of many of us to try to shove our own code when someone asks for help. Please don't interpret it as arrogance. In this case specifically it is my inability to debug your code, especially since I didn't translate mine from a math formula, like you did, but merely from another programming language, which was much easier than the task you set out to do.
I hope it is at least helpful as a comparison source.
Title: Re: Problem with HSL to RGB function
Post by: johannhowitzer on February 17, 2020, 01:58:31 pm
I found part of the problem, but there's still some math to fix. I was assuming the mod operator would return a floating point value, and QB64's mod operator specifically only returns integers. So I just coded my own function, and now I'm getting gradual changes, but still not quite correct.
This is producing gradient bands separated by a sharp edge, because the x value is rising from 0 to 0.5 in the red-to-yellow part, then continuing from 0.5 to 1 in the yellow-to-green part. The secondary color component is rising from 0 to 1 across twice the range it should be, and starting abruptly again at 0 instead of traveling gradually back down. So I know what needs to happen from here. Thankfully, saturation and luminance are both behaving correctly.
However, I've been picking apart the math for an hour and am getting a headache, and my formula matches both the page I linked and the wikipedia article on conversion, so I'm taking a break for now. If anyone wants to check this out and see if they can correct the math before me, have at it!
Title: Re: Problem with HSL to RGB function
Post by: johannhowitzer on February 21, 2020, 01:47:50 pm
Found the error. It wasn't in the HSL function itself - I'd just made a simple mistake in coding my own mod function. My function mod_dec() in the above post compares the division result to the division result rounded, in order to get the remainder - however, after this, I neglected to multiply this remainder by the divisor. Example: in the case of 8 mod 3, the result should be 2, but as I was taking the difference of (8 / 3) and int(8 / 3), I would first get a result of 2/3. So here's the corrected function:
The HSL to RGB function now works correctly. My first mistake was assuming QB64's mod operator preserved decimals, and my second was in coding my own mod function, I forgot to multiply by the divisor at the end. Always check your functions. :)
Title: Re: Problem with HSL to RGB function
Post by: bplus on February 21, 2020, 02:14:07 pm
Hi johannhowitzer,
That's pretty nice. I fooled around with rainbow range and could get either to Black or to White but not both.
Title: Re: Problem with HSL to RGB function
Post by: johannhowitzer on February 21, 2020, 08:56:13 pm
I'm not sure I understand what you mean. What variables did you change in the input?
Title: Re: Problem with HSL to RGB function
Post by: bplus on February 21, 2020, 09:22:50 pm
Just trying to get a color picker with ranges top dark, bottom light. Forget about hsl standing for anything except h for hue:
Title: Re: Problem with HSL to RGB function
Post by: johannhowitzer on February 21, 2020, 09:46:57 pm
OK, couple things...
- That's not the HSL function I wrote, if you're trying to get eyes on debugging your own function, it might be more helpful to start a new topic, and I'm not sure from looking at that function what it's supposed to do, so I might not be able to help you.
- I'm using QB64 1.0, I should probably update because I bet _PI is in a newer version, and my version doesn't like that it has two different argument setups.
- Seems weird that you're asking about light / dark when you're passing the "gray" value into two parameters of your function. It doesn't look like you're doing anything at all with lightness here. If you're trying to vertically invert everything visually, try subtracting the y coordinate of pset from the y coordinate you want as the bottom edge.
Title: Re: Problem with HSL to RGB function
Post by: bplus on February 21, 2020, 11:58:25 pm