Last active
November 22, 2022 11:25
-
-
Save andrew-wilkes/b37ddc33c0b4f049ab121ecbbfd480ef to your computer and use it in GitHub Desktop.
GDScript function to convert RGB color to HSV values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 } |
we can make code golf? (jk)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?