colournamer

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2023 License: MIT Imports: 4 Imported by: 0

README

🎨 Colour Namer

This is a Go port of the Node.js color-namer package and was inspired by the random color contrasts bot on Mastodon.

From a given colour input — either an HTML hex code (#00ff00) or an RGB value (0, 128, 255) — it computes the closest colour values from a variety of colour lists:

  • Basic
  • HTML - the HTML colour names
  • NTC - a collection of named colours
  • Pantone
  • ROYBGIV - red, orange, yellow, blue, green, indigo, violet

Usage

Fetch the module:

go get github.com/jamesog/colournamer

Add it as an import and use one of the From functions:

import "github.com/jamesog/colournamer"

colournamer.FromHex("#008080")
colournamer.FromRGB(0, 128, 128)

Both functions return a Results struct containing each of the lists, with each list containing an array of colours sorted by how close they are to the given colour value.

CLI Tool

The cmd/colournamer tool lets you use the package as a CLI tool:

colournamer #008080 'rgb(0, 128, 128)'

The tool will accept either HTML hex values (preceded by #) or RGB values in the form rgb(0, 0, 0). Spaces are optional in the RGB form.

FAQs

Why?

I liked the random color contrasts bot and had been generating colours from Randoma11y and wanted to name them like the bot does.

I didn't want to have to install Node and NPM and have thousands of Node packages cluttering my filesystem.

Colours? It's spelled colors!

I'm English.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Basic = []Colour{
		{Name: "black", Colour: colorful.Color{R: 0, G: 0, B: 0}},
		{Name: "blue", Colour: colorful.Color{R: 0, G: 0, B: 1}},
		{Name: "cyan", Colour: colorful.Color{R: 0, G: 1, B: 1}},
		{Name: "green", Colour: colorful.Color{R: 0, G: 0.5019607843137255, B: 0}},
		{Name: "teal", Colour: colorful.Color{R: 0, G: 0.5019607843137255, B: 0.5019607843137255}},
		{Name: "turquoise", Colour: colorful.Color{R: 0.25098039215686274, G: 0.8784313725490196, B: 0.8156862745098039}},
		{Name: "indigo", Colour: colorful.Color{R: 0.29411764705882354, G: 0, B: 0.5098039215686274}},
		{Name: "gray", Colour: colorful.Color{R: 0.5019607843137255, G: 0.5019607843137255, B: 0.5019607843137255}},
		{Name: "purple", Colour: colorful.Color{R: 0.5019607843137255, G: 0, B: 0.5019607843137255}},
		{Name: "brown", Colour: colorful.Color{R: 0.6470588235294118, G: 0.16470588235294117, B: 0.16470588235294117}},
		{Name: "tan", Colour: colorful.Color{R: 0.8235294117647058, G: 0.7058823529411764, B: 0.5490196078431373}},
		{Name: "violet", Colour: colorful.Color{R: 0.9333333333333333, G: 0.5098039215686274, B: 0.9333333333333333}},
		{Name: "beige", Colour: colorful.Color{R: 0.9607843137254902, G: 0.9607843137254902, B: 0.8627450980392157}},
		{Name: "fuchsia", Colour: colorful.Color{R: 1, G: 0, B: 1}},
		{Name: "gold", Colour: colorful.Color{R: 1, G: 0.8431372549019608, B: 0}},
		{Name: "magenta", Colour: colorful.Color{R: 1, G: 0, B: 1}},
		{Name: "orange", Colour: colorful.Color{R: 1, G: 0.6470588235294118, B: 0}},
		{Name: "pink", Colour: colorful.Color{R: 1, G: 0.7529411764705882, B: 0.796078431372549}},
		{Name: "red", Colour: colorful.Color{R: 1, G: 0, B: 0}},
		{Name: "white", Colour: colorful.Color{R: 1, G: 1, B: 1}},
		{Name: "yellow", Colour: colorful.Color{R: 1, G: 1, B: 0}},
	}
	HTML = []Colour{}/* 147 elements not displayed */

	NTC = []Colour{}/* 1566 elements not displayed */

	Pantone = []Colour{}/* 120 elements not displayed */

	ROYGBIV = []Colour{
		{Name: "red", Colour: colorful.Color{R: 1, G: 0, B: 0}},
		{Name: "orange", Colour: colorful.Color{R: 1, G: 0.6470588235294118, B: 0}},
		{Name: "yellow", Colour: colorful.Color{R: 1, G: 1, B: 0}},
		{Name: "green", Colour: colorful.Color{R: 0, G: 0.5019607843137255, B: 0}},
		{Name: "blue", Colour: colorful.Color{R: 0, G: 0, B: 1}},
		{Name: "indigo", Colour: colorful.Color{R: 0.29411764705882354, G: 0, B: 0.5098039215686274}},
		{Name: "violet", Colour: colorful.Color{R: 0.9333333333333333, G: 0.5098039215686274, B: 0.9333333333333333}},
	}
)

Functions

This section is empty.

Types

type Colour

type Colour struct {
	Name     string
	Colour   colorful.Color
	Distance float64
}

func (Colour) Hex

func (c Colour) Hex() string

Hex is the HTML hex representation of the colour.

func (Colour) String

func (c Colour) String() string

type Results

type Results struct {
	Basic   []Colour
	HTML    []Colour
	NTC     []Colour
	Pantone []Colour
	ROYBGIV []Colour
}

Results contains all colour matches, with each list containing an array of colours sorted by their similarity to the given colour.

func FromHex

func FromHex(hex string) (Results, error)

FromHex takes a hex RGB string and returns the closest colour name.

func FromRGB

func FromRGB(r, g, b uint8) (Results, error)

FromRGB takes RGB values and returns the closest colour name.

The alpha value is set to 1. Use FromRGBA if you need to control the alpha.

func FromRGBA

func FromRGBA(r, g, b, a uint8) (Results, error)

FromRGBA takes RGBA values and returns the closest colour name.

func (Results) ClosestName

func (r Results) ClosestName() string

ClosestName returns the name of the colour whose RGB value is closest to the original value.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL