vsupalov

Generate a distinctive color palette

November 10, 2013

Recently I needed a set number of distinctive colors (16) for a data visualization task. Here is some Go code, that uses the marvelously documented go-colorful library:

package main

import (
    "fmt"
    "os"
    "strings"
    "github.com/lucasb-eyer/go-colorful"
)

var labels = [...]string{
    "Laning Presence","Jungle","Defend","Push","Escape",
    "Gank","Farm","Stack","Pull","Teamfight","Roshan",
    "Ward","Counterward","Harass","Contest Pull","Fight"}

func main() {
    num_colors := len(labels)

    pal, err := colorful.HappyPalette(num_colors)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    colors := make([]string, num_colors)
    for i := 0; i < num_colors; i++ {
        colors[i] = fmt.Sprintf("\"%v\": \"%v\"", labels[i],pal[i].Hex())
    }

    fmt.Printf("colors = {\n")
    fmt.Printf("    %v\n",strings.Join(colors, ", "))
    fmt.Printf("}\n")
}

The output of this code is a JavaScript dictionary. The same approach can be used for any other language where you do not wish to deal with the hassle of generating colors during runtime.

Join the mailing list!


Subscribe to get notified about future articles and stay in touch via email.

I write about Django, Kubernetes, Docker, automation- and deployment topics, but would also like to keep you up to date about news around the business-side of things.

Privacy and your data: You can get more information about the usage of your data, the storage of your registration, sending out mails with the US-provider ConvertKit, statistical analysis of emails sent and your possibility to unsubscribe in my Privacy Policy.

I use the US-provider ConvertKit for email automation. By clicking to submit this form, you acknowledge that the information you provide will be transferred to ConvertKit for processing in accordance with their Privacy Policy and Terms.

We won't send you spam. Unsubscribe at any time. Powered by ConvertKit