Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
python3

What is colorsys.rgb_to_hsv(r, g, b) in Python?

Syed Muhammad Ali Mustafa

The colorsys.rgb_to_hsv(r, g, b) function converts RGBRed Green Blue coordinates to HSVHue Saturation Value coordinates.

Declaration

The colorsys module stores the definition of the rgb_to_hsv() function. We can import the colorsys module using the following code:

import colorsys

colorsys.rgb_to_hsv(r, g, b)

r, g, and b can have any value between 0 and 1 inclusive.

Code

The following code snippet demonstrates the use of rgb_to_hsv():

import colorsys

if __name__ == '__main__':
  r = 152
  g = 200
  b = 40

  ## normalizing values to get value between 0 and 1
  r = r/255
  g = g/255
  b = b/255

  h, s, v = colorsys.rgb_to_hsv(r, g, b)

  print('RGB:-')
  print('r: ' + str(r) + '\ng: ' + str(g) + '\nb: ' + str(b))
  print('\nHSV:-')
  print('h: ' + str(h) + '\ns: ' + str(s) + '\nv: ' + str(v))

RELATED TAGS

python
python3

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring