It is currently Mon Apr 15, 2024 7:31 am


All times are UTC - 5 hours [ DST ]


Switch to mobile style

Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: RGB to HSV - single colour
PostPosted: Tue Dec 09, 2014 3:28 pm  (#1) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
GIMP Version: 2.8.14
Operating System: Windows
GIMP Experience: Beginner Level



Hi all scripters in scheme.
My question today is the following:
- is there an already available routine written in scheme to convert a (single) colour from the RGB values to the HSV values?
I know there is in python a function available (colorsys.rgb_to_hsv), is there an equivalent scheme routine/function?
Thanks a lot

_________________
"Where am I ?"


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Tue Dec 09, 2014 5:02 pm  (#2) 
Offline
GimpChat Founder
User avatar

Joined: May 22, 2008
Posts: 5242
Location: Gimpville
Take a look at Glass Effect Text V1.4. RGB to HSV functions are included in there.

_________________
“If you reach for the stars, you just might land on a decently sized hill.” - Stuart Hill


Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Tue Dec 09, 2014 5:06 pm  (#3) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
thanks a lot GT, I will have a look at your script (tomorrow, now it's time for ...bed!)

_________________
"Where am I ?"


Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Tue Dec 09, 2014 5:22 pm  (#4) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
Here is my own conversion code. All resulting HSV values are normalized to 0-255.

(define (rgb->hsv r g b)
  (let* ((v (max r g b))
         (d (- v (min r g b)))
         (s (if (zero? v)
              0
              (* 255 (/ d v)) ))
         (h (cond
              ((zero? s) 0)
              ((= r v) (* 42.5 (/ (- g b) d)))
              ((= g v) (+ 85 (* 42.5 (/ (- b r) d))))
              (else (+ 170 (* 42.5 (/ (- r g) d)))) )))
    (list (if (< h 0)
            (+ h 255)
            h )
          s
          v )))

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Last edited by saulgoode on Tue Dec 09, 2014 5:38 pm, edited 2 times in total.

Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Tue Dec 09, 2014 5:25 pm  (#5) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
many thanks, Saul!
as previously said to GT, i'm going to sleep, now...
tomorrow I will dedicate time to these routines

_________________
"Where am I ?"


Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Tue Dec 09, 2014 7:27 pm  (#6) 
Offline
GimpChat Founder
User avatar

Joined: May 22, 2008
Posts: 5242
Location: Gimpville
dinasset wrote:
thanks a lot GT, I will have a look at your script (tomorrow, now it's time for ...bed!)

The original script is mine but the RGB to HSV routines were added to the script by someone else.

Personally, I prefer Saul's solution over the logic included in the glass script.

_________________
“If you reach for the stars, you just might land on a decently sized hill.” - Stuart Hill


Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Wed Dec 10, 2014 1:46 am  (#7) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
OK GT, I will use Saul's provided routine.
Saul's style is always the best in scheme, he is really a scheme guru!

_________________
"Where am I ?"


Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Thu Dec 11, 2014 1:53 pm  (#8) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
Saul,
I'm using your routine in a script I'm experimenting now.
Because I use the output values as input to gimp colorize, I adjusted the 2nd and 3rd value to be in the range 100 instead of 256 (I simply divided those two output values by 2.55).
But -googling- I read that HSV and HSL differ technically and colorize wants values in HSL.
So far -first experiments- I saw good results using your HSV values, but do you think that the routine has to be changed to return exactly HSL values?
Thanks

_________________
"Where am I ?"


Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Thu Dec 11, 2014 2:44 pm  (#9) 
Offline
Global Moderator
User avatar

Joined: Nov 16, 2011
Posts: 5128
Location: Metro Vancouver, BC
A couple of links that might be worth a look.
Bruce Lindbloom's site has very good technical color information.
EasyRGB Color conversion math and formulas (some Gimp specific)

_________________
Image
Gimp 2.8.18, Linux, median user
Gimp Chat Tutorials Index
Spirit Bear (Kermode)


Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Thu Dec 11, 2014 2:53 pm  (#10) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
thanks, Odin!
I will try to translate the code into scheme...

_________________
"Where am I ?"


Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Thu Dec 11, 2014 6:56 pm  (#11) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
Here's the code I came up with, though I haven't thoroughly tested it. Again all outputs normalized to 0-255.

(define (rgb->hsl r g b)
  (let* ((vmax (max r g b))
         (vmin (min r g b))
         (d (- vmax vmin))
         (l (/ (+ vmax vmin) 2))
         (s (if (zero? d)
              0
              (* 255 (if (< l 128)
                       (/ d (+ vmax vmin))
                       (/ d (- 1 (abs (- (* 2 l) 1)))) ))))
         (h (cond
              ((zero? d) 0)
              ((= r vmax)        (* 42.5 (/ (- g b) d)))
              ((= g vmax) (+ 85  (* 42.5 (/ (- b r) d))))
              (else       (+ 170 (* 42.5 (/ (- r g) d)))) )))
    (list (if (< h 0)
            (+ h 255)
            h )
          s
          l )))

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Fri Dec 12, 2014 2:16 am  (#12) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
thanks a lot, Saul!
this will save me time and pain in performing the translation from the formula to scheme myself..
as before, for "colorize" I will normalize from 255 to 100 the 2.nd and 3.rd value, simply dividing by 2.55
thanks again

_________________
"Where am I ?"


Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Fri Dec 12, 2014 4:12 am  (#13) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
Saul,
I replaced your old version with the new one and I get an error due to the fact that sometimes the saturation value is set to a negative value; in "colorize" the hsl values accepted are in the range -100 to 100 only for the third value (lightness) while the 2nd value (saturation) should be in the range 0-100
can I do myself some modification (in addition to the trivial one I do now, i.e. dividing 2nd and 3rd value by 2.55)? which one please?

_________________
"Where am I ?"


Top
 Post subject: Re: RGB to HSV - single colour
PostPosted: Sat Dec 13, 2014 7:42 am  (#14) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
addendum to my last post

Saul,

I made a little modification to your routine:
replaced the line
(/ d (- 1 (abs (- (* 2 l) 1)))) ))))
with
(/ d (- 512 (+ vmax vmin))) ))))

I made also another little change, after exiting your routine:
because ALL values you provide are in range 0-255 (as you indicated) in addition to divide value 2(S) and 3(L) by 2.55 to get range 100 I also applied to value 1(H) a factor 360/255, because colorize requires hue in the range 360

it seems be working, but please check whether I did the correct modifications
thanks Saul

_________________
"Where am I ?"


Top
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
This topic is locked, you cannot edit posts or make further replies. Attachment(s) Interlace single letter text with a single layer? [Solved]

8

No new posts Attachment(s) How to structure single color areas

3

No new posts Single Line Filled Anemone.

5

No new posts Selection pasting only transparency - single layer image [Solved]

3

No new posts Attachment(s) Colour Cast Script

0



* Login  



Powered by phpBB3 © phpBB Group