It is currently Thu Jul 16, 2026 4:58 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: getting the HUE of a pixel
PostPosted: Thu Dec 03, 2015 10:37 am  (#1) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14831
Location: roma, italy
GIMP Version: 2.8.14
Operating System: Windows
GIMP Experience: New User



Hi all.
Is there a native of add-on function to get the HUE of a pixel?
If not, because I read by googling that it's possible to derive the HUE from R,G,B

Preucil[7] describes a color hexagon, similar to a trilinear plot described by Evans, Hanson, and Brewer,[8] which may be used to compute hue from RGB. To place red at 0°, green at 120°, and blue at 240°,

hue(rgb) = atan2(sqrt{3} * (G - B), 2 * R - G - B ).


can some guru "translate" this formula in python?
Thanks!

(I don't know whether I reported the formula correctly, but experts will know it)

_________________
"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: getting the HUE of a pixel
PostPosted: Thu Dec 03, 2015 4:21 pm  (#2) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14831
Location: roma, italy
edit:

continuing googling, after the above calculation it's necessary to multiply the result by 60 then if negative add 360

the previously indicated formula in python is easy,:
"import math"
"hue1 = math.atan2(math.sqrt(3) * (G - B), 2 * R - G - B)"

(then we multiply by 60 and add 360 if necessary)

experimented on python the result seems to be approximate IMO:
- an RGB with a value only on Green gives Hue=125.6...(shouldn't be 120?)
- an RGB with a value only on Blue gives Hue=234.3....(shouldn't be 240?)
when the RGB has value only on R the Hue is exact: 0.0

there is also a second formula, but it gives the same results.

is there something better around?

_________________
"Where am I ?"


Top
 Post subject: Re: getting the HUE of a pixel
PostPosted: Thu Dec 03, 2015 7:42 pm  (#3) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
For what it's worth, here is my Scheme function for converting from RGB to HSV. All inputs and outputs should be normalized to fall within the range 0.0 to 1.0 .

; (rgb->hsv r g b) ==> (h s v)
; inputs and outputs all normalized from 0.0 to 1.0
;
(define rgb->hsv
  (lambda rgb
    (let* ((v (apply max rgb))
           (d (- v (apply min rgb)))
           (s (if (zero? v)
                0.0
                (/ d v)))
           (h (cond
                ((zero? v)
                  0.0)
                ((< d 0.00001)
                  v)
                ((= (car rgb) v)
                  (/ (- (cadr rgb) (caddr rgb)) 6 d))
                ((= (cadr rgb) v)
                  (+ (/ 3) (/ (- (caddr rgb) (car rgb)) 6 d)))
                ((= (caddr rgb) v)
                  (+ (/ 2 3) (/ (- (car rgb) (cadr rgb)) 6 d))))))
      (list (if (< h 0)
              (+ h 1)
              h)
            s
            v))))

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: getting the HUE of a pixel
PostPosted: Thu Dec 03, 2015 7:53 pm  (#4) 
Offline
Script Coder
User avatar

Joined: Jul 14, 2010
Posts: 697
And here are the scheme functions I use to convert back and forth ('cause I can't think in Lisp like SG can... :P):
(define (huefromrgb R G B)
  (let* ()
    (cond
      ((= R G B) 0)
     ((and (>= R G) (>= G B)) (round (* 60 (/ (- G B) (- R B)))))
     ((and (> G R) (>= R B)) (round (* 60 (- 2 (/ (- R B) (- G B))))))
     ((and (>= G B) (> B R)) (round (* 60 (+ 2 (/ (- B R) (- G R))))))
     ((and (> B G) (> G R)) (round (* 60 (- 4 (/ (- G R) (- B R))))))
     ((and (> B R) (>= R G)) (round (* 60 (+ 4 (/ (- R G) (- B G))))))
     ((and (>= R B) (> B G)) (round (* 60 (- 6 (/ (- B G) (- R G))))))
    )))
   
(define (rgbfromhue hue)
  (let* ()
    (list
    ; Red
    (round (cond
      ((< hue 60) 255)
      ((< hue 120) (* 255 (- 1 (/ (- hue 60) 60))))
      ((< hue 240) 0)
      ((< hue 300) (* 255 (/ (- hue 240) 60)))
      ((< hue 360) 255)
    ))
    ; Green
    (round (cond
      ((< hue 60) (* 255 (/ hue 60)))
      ((< hue 180) 255)
      ((< hue 240) (* 255 (- 1 (/ (- hue 180) 60))))
      ((< hue 360) 0)
    ))
    ; Blue
    (round( cond
      ((< hue 120) 0)
      ((< hue 180) (* 255 (/ (- hue 120) 60)))
      ((< hue 300) 255)
      ((< hue 360) (* 255 (- 1 (/ (- hue 300) 60))))
    ))
    )
  )
)


-Rob A>

_________________
Image
Fantasy Cartography and Mapping by RobA


Top
 Post subject: Re: getting the HUE of a pixel
PostPosted: Fri Dec 04, 2015 12:24 am  (#5) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14831
Location: roma, italy
thanks a lot, Saul and Rob!
I'll "translate" into python (or call as they are...)

_________________
"Where am I ?"


Top
 Post subject: Re: getting the HUE of a pixel
PostPosted: Fri Dec 04, 2015 2:40 pm  (#6) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14831
Location: roma, italy
@RobA
Rob, I'm experimenting your formula, first part (from RGB to hue), after a "translation" to python.
One comment: unless I'm misinterpreting your formula, at the end should be necessary IMO to add a statement: if resulting hue is negative, then multiply it by -1 (in other words: it's the absolute value which represents the hue).
I say that because in my first experiments while the red, blue, cyan and yellow colors return a positive (and corresponding) value, respectively 0, 240, 180, 60, the green and magenta colors return a value which is correct in absolute value but with a minus in front, that is -120 and -300.
Am I wrong?

_________________
"Where am I ?"


Top
 Post subject: Re: getting the HUE of a pixel
PostPosted: Fri Dec 04, 2015 7:02 pm  (#7) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4503
Location: Canada
I am curious to know what you're working on dinasset.

_________________
TinT


Top
 Post subject: Re: getting the HUE of a pixel
PostPosted: Sat Dec 05, 2015 1:10 am  (#8) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14831
Location: roma, italy
an easy way to identify a colour by one single value, to understand "colours proximity"

_________________
"Where am I ?"


Top
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]



* Login  



Powered by phpBB3 © phpBB Group