Skip to content

Commit 7e051af

Browse files
committed
add util.RGB_to_HSV
1 parent a5bd9da commit 7e051af

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

Functional.moon

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,17 @@ _util = {
715715

716716
return _re.replace format, "(h+|m+|s+|f+)", (flag) ->
717717
_string.pad tostring(splits[flag\sub 1, 1]), #flag
718+
719+
RGB_to_HSV: (r, g, b) ->
720+
r, g, b = util.clamp(r, 0, 255), util.clamp(g, 0, 255), util.clamp(b, 0, 255)
721+
v = math.max r, g, b
722+
delta = v - math.min r, g, b
723+
if delta == 0
724+
return 0, 0, v/255
725+
else
726+
s = delta/v
727+
h = 60*(r == v and (g-b)/delta or g == v and (b-r)/delta + 2 or (r-g)/delta + 4)
728+
return h > 0 and h or h+360, s, v/255
718729
}
719730

720731
_unicode = {

0 commit comments

Comments
 (0)