Skip to content

Instantly share code, notes, and snippets.

@andrew-wilkes
Last active November 22, 2022 11:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrew-wilkes/b37ddc33c0b4f049ab121ecbbfd480ef to your computer and use it in GitHub Desktop.
Save andrew-wilkes/b37ddc33c0b4f049ab121ecbbfd480ef to your computer and use it in GitHub Desktop.
GDScript function to convert RGB color to HSV values
# I/O values are in the range 0.0 .. 1.0
func rgb_to_hsv(c: Color):
var M: float = rgb.max()
var m: float = rgb.min()
var v = M
var s = 0.0 if M < 0.001 else 1.0 - m / M
var h = (c.r - c.g / 2.0 - c.b / 2.0) / sqrt(c.r*c.r + c.g*c.g + c.b*c.b - c.r*c.g - c.r*c.b - c.g*c.b)
return { h=h, s=s, v=v }
@andrew-wilkes
Copy link
Author

It transpires that we don't need this in Godot since the Color class has properties of h, s, v. Something that I missed, but this code is maybe interesting to look at?

@RichardEllicott
Copy link

we can make code golf? (jk)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment