Created
June 23, 2019 16:59
-
-
Save Bios-Marcel/a46d4e944aab48e5c5b8f98352890c89 to your computer and use it in GitHub Desktop.
Little RGB to HSL converter I wrote just to see how hard it would be.
This file contains 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 ( | |
"flag" | |
"fmt" | |
"math" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
flag.Parse() | |
input := strings.Split(flag.Arg(0), ",") | |
var iR, lowR, iG, lowG, iB, lowB float64 | |
iR, _ = strconv.ParseFloat(input[0], 8) | |
iG, _ = strconv.ParseFloat(input[1], 8) | |
iB, _ = strconv.ParseFloat(input[2], 8) | |
lowR = iR / 255 | |
lowG = iG / 255 | |
lowB = iB / 255 | |
min := math.Min(math.Min(lowR, lowG), lowB) | |
max := math.Max(math.Max(lowR, lowG), lowB) | |
var H, S, L float64 | |
L = (min + max) / 2 | |
if min == max { | |
H = 0 | |
S = 0 | |
} else { | |
maxSubMin := max - min | |
if L < 0.5 { | |
S = maxSubMin / (max + min) | |
} else { | |
S = maxSubMin / (2.0 - max - min) | |
} | |
if max == lowR { | |
H = (lowG - lowB) / maxSubMin | |
} else if max == lowG { | |
H = 2.0 + (lowB-lowR)/maxSubMin | |
} else { | |
H = 4.0 + (lowR-lowG)/maxSubMin | |
} | |
H *= 60 | |
} | |
fmt.Printf("%.0f,%.1f,%.1f\n", math.Round(H), math.Round(S*1000)/10, math.Round(L*1000)/10) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment