Last active
June 13, 2017 11:13
-
-
Save henryhamon/c3344463166312d6b82770e831222e8e to your computer and use it in GitHub Desktop.
Random color generator in Golang
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
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func rainbow(numOfSteps, step float64) (int, int, int) { | |
var r, g, b float64 | |
h := step / numOfSteps | |
i := math.Floor(h * 6) | |
f := h*6 - i | |
q := 1 - f | |
os := math.Remainder(i, 6) | |
fmt.Println(os, h, i, f, q) | |
switch os { | |
case 0: | |
r = 1 | |
g = f | |
b = 0 | |
case 1: | |
r = q | |
g = 1 | |
b = 0 | |
case 2: | |
r = 0 | |
g = 1 | |
b = f | |
case 3: | |
r = 0 | |
g = q | |
b = 1 | |
case 4: | |
r = f | |
g = 0 | |
b = 1 | |
case 5: | |
r = 1 | |
g = 0 | |
b = q | |
} | |
r = r * 255 | |
g = g * 255 | |
b = b * 255 | |
return int(r), int(g), int(b) | |
} | |
func main() { | |
fmt.Println(rainbow(50, 30)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment