colorcode

package module
v0.0.0-...-80ec1da Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2023 License: MIT Imports: 3 Imported by: 1

README

test and Benchmark Coverage Status Go Report Card Documentation

colorcode

A library of color spaces and conversions written in Go 🎨

Get the library

Getting the code down is as simple as running go get github.com/YashdalfTheGray/colorcode and then importing into your code with import "github.com/YashdalfTheGray/colorcode".

What's included?

This library comes with a few different color spaces to play around with - RGB + hex code colors, HSL, and HSV. Those are the most common ones that require conversions. Feel free to open an issue if you would like to see a color space added to this library.

Usage

Each color space, RGB, hex, HSL and HSV give you a way to construct a color of that type by passing in the channels required. The package comes with methods that start with New that let you create the colors. For example, to create a new RGB color, you would use colorcode.NewRGB(127, 127, 127).

Furthermore, each of the color spaces extend fmt.Stringer if you ever wanted a string representation of them. On instances that are returned from the New methods, you will find To methods which are used to convert between color spaces. For example, rgbColor.ToHSV() will return an HSV color and hexColor.toHSL() will return an HSL color. Some color conversions throw errors because the library wants you to know that something went wrong outside of the math that we're using to convert.

An example is below.

package main

import "github.com/yashdalfthegray/colorcode"

func main() {
  hexColor := colorcode.NewHexCode("#deadaf")
  rgbColor, err := hexColor.ToRGB()
  if err != nil {
    hslColor := rgbColor.ToHSL()
    hsvColor := hslColor.ToHSV()
  }
}

Contributing

Feel free to use Github to open issues and create PRs against this repository. There are templates that you can use to describe issues and PRs as needed. Please follow the guidelines in the contribution guide for development. This project uses Go 1.17 with standard tooling and Go modules.

Documentation

Overview

Package colorcode provides color space definitions and methods to convert between them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HSL

type HSL struct {
	H, S, L float64
}

HSL represents a color in the HSL coordinate system, having 8 bits for each of hue, saturation, and luminosity. Implements fmt.Stringer

func NewHSL

func NewHSL(h, s, l float64) (HSL, error)

NewHSL returns a new HSL object constructured out of the values given to the constructor function

func (HSL) String

func (c HSL) String() string

String returns a string representation of the HSL object

func (HSL) ToHSV

func (c HSL) ToHSV() HSV

ToHSV returns an HSV representation of the HSL color

func (HSL) ToHexCode

func (c HSL) ToHexCode() HexCode

ToHexCode returns a hex color string equivalent of the HSL color

func (HSL) ToRGB

func (c HSL) ToRGB() RGB

ToRGB converts an HSL color to its RGB representation

type HSV

type HSV struct {
	H, S, V float64
}

HSV represents a color in the HSV color space. This also implements the fmt.Stringer interface

func NewHSV

func NewHSV(h, s, v float64) (HSV, error)

NewHSV takes values for H, S and V and returns an HSV type

func (HSV) String

func (c HSV) String() string

String returns a string representation of the HSV object

func (HSV) ToHSL

func (c HSV) ToHSL() HSL

ToHSL converts the HSV color into the HSL representation

func (HSV) ToHexCode

func (c HSV) ToHexCode() HexCode

ToHexCode converts the HSV color into the HexCode representation

func (HSV) ToRGB

func (c HSV) ToRGB() RGB

ToRGB converts the HSV color to the RGB representation

type HexCode

type HexCode string

HexCode represents an HTML color string. Implements fmt.Stringer

func NewHexCode

func NewHexCode(code string) HexCode

NewHexCode returns a new HexCode built out of a string

func (HexCode) String

func (cs HexCode) String() string

String returns the string representation of the HexCode

func (HexCode) ToHSL

func (cs HexCode) ToHSL() (HSL, error)

ToHSL converts a HexCode string color to its HSL equivalent

func (HexCode) ToHSV

func (cs HexCode) ToHSV() (HSV, error)

ToHSV converts a HexCode string color to its HSV equivalent

func (HexCode) ToRGB

func (cs HexCode) ToRGB() (RGB, error)

ToRGB converts a HexCode string color into its RGB equivalent

type RGB

type RGB struct {
	R, G, B uint8
}

RGB represents a traditional 24-bit color, having 8 bits for each of red, green, and blue. Implements fmt.Stringer

func NewRGB

func NewRGB(r, g, b uint8) RGB

NewRGB returns a new RGB object constructured out of the values given to the constructor function

func (RGB) String

func (c RGB) String() string

String returns a string representation of the RGB object

func (RGB) ToHSL

func (c RGB) ToHSL() HSL

ToHSL converts an RGB color to the HSL representation

func (RGB) ToHSV

func (c RGB) ToHSV() HSV

ToHSV returns the HSV representation of the RGB color

func (RGB) ToHexCode

func (c RGB) ToHexCode() HexCode

ToHexCode returns a Hex color string equivalent of the the RGB color

Jump to

Keyboard shortcuts

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