Color Converter for Go
Free online color converter with Go code examples
Working with color converter in Go? Our free online color converter helps Go developers format, validate, and process data instantly. Below you will find Go code examples using image/color (built-in) so you can achieve the same result programmatically in your own projects.
Try the Color Converter Online
Use our free Color Converter directly in your browser — no setup required.
Open Color ConverterGo Code Example
package main
import (
"fmt"
"math"
"strconv"
)
func hexToRGB(hex string) (r, g, b uint8) {
val, _ := strconv.ParseUint(hex[1:], 16, 32)
return uint8(val >> 16), uint8(val >> 8 & 0xFF), uint8(val & 0xFF)
}
func rgbToHSL(r, g, b uint8) (h, s, l float64) {
rf, gf, bf := float64(r)/255, float64(g)/255, float64(b)/255
max := math.Max(rf, math.Max(gf, bf))
min := math.Min(rf, math.Min(gf, bf))
l = (max + min) / 2
if max == min { return 0, 0, l }
d := max - min
if l > 0.5 { s = d / (2 - max - min) } else { s = d / (max + min) }
switch max {
case rf: h = (gf - bf) / d; if gf < bf { h += 6 }
case gf: h = (bf - rf) / d + 2
case bf: h = (rf - gf) / d + 4
}
return h * 60, s, l
}
func main() {
r, g, b := hexToRGB("#3498db")
fmt.Printf("RGB: (%d, %d, %d)\n", r, g, b)
h, s, l := rgbToHSL(r, g, b)
fmt.Printf("HSL: (%.0f, %.0f%%, %.0f%%)\n", h, s*100, l*100)
}Quick Setup
Library: image/color (built-in)
// Built-in package — no installation neededGo Tips & Best Practices
- image/color provides basic RGBA and CMYK types
- Custom functions are needed for HSL/HSV conversions
- github.com/lucasb-eyer/go-colorful has rich color support