Rock E Rolla
Rock E Rolla

Reputation: 39

PyQt5 QColor conversion from HSV

I am doing a HSV color picker for Krita that has PyQt5. I wanted to make use of the QColor class to take care of display color conversions, however i am not able to convert the color with success.

I used this color selector as a reference for my output:

https://www.google.com/search?sxsrf=ACYBGNR9_2R1jGyxkqbdM8DVZfU-8hogYg%3A1583142187803&source=hp&ei=K9VcXqrKLuyMlwTorLH4Dg&q=color+picker+hex&oq=color+picker&gs_l=psy-ab.3.0.35i39l2j0l8.3060.6761..8026...2.0..0.310.1102.11j3-1......0....1..gws-wiz.....10..35i362i39j0i10.C-faeoukSfM

a random color of choice was:

HEX=(#eb4034)
HSV=(7, 78, 92)

When I try to convert it with this code:

from PyQt5.QtGui import QColor

hsv = QColor.fromHsv(7,78,92,255)
color = str(hsv.redF()*255)+" | "+str(hsv.greenF()*255)+" | "+str(hsv.blueF()*255)

print(str( color ))

my output is:

92.0 | 67.14007782101167 | 63.85992217898833
[Finished in 0.168s]

instead of:

235, 64, 52

How do I input a HSV color into the QColor and then convert it to RGB for display purposes?

thank you in advance

Upvotes: 0

Views: 1301

Answers (2)

Heike
Heike

Reputation: 24430

The reference values for the HVS color (i.e. (7, 78, 92)) are in units degree, percent, and percent, respectively, whereas QColor.fromHSV expects values in the range 0-255. To convert the reference values to something that can be used by QColor you could try something like this

HSV=(7, 78, 92)
hue, sat, value = HSV
hue = int(hue*255/360)
sat = int(sat*255/100)
value = int(value*255/100)

hsv = QColor.fromHsv(hue, sat, value, 255)
color = str(hsv.redF()*255)+" | "+str(hsv.greenF()*255)+" | "+str(hsv.blueF()*255)

print(str( color ))

# output: 234.0 | 64.42023346303502 | 52.307392996108945

Upvotes: 1

alec
alec

Reputation: 6112

You can use the getRgb() method to get a tuple with the values:

>>> hsv = QColor.fromHsv(7,78,92,255)
>>> hsv.getRgb()
(92, 67, 64, 255)

You will notice the values are different than what you expected, this is because the color hex #eb4034 is not the same as HSV (7, 78, 92).

>>> hsv.name()
'#5c4340'
>>> QColor('#eb4034').getHsv()
(3, 199, 235, 255)

Make sure you are using the correct HSV values.

Upvotes: 1

Related Questions