colors

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: MIT Imports: 7 Imported by: 12

README

Package colors

Project status GoDoc

Go color manipulation, conversion and printing library/utility

this library is currently in development, not all color types such as HSL, HSV and CMYK will be included in the first release; pull requests are welcome.

Installation

Use go get.

go get github.com/go-playground/colors

Then import the validator package into your own code.

import "github.com/go-playground/colors"

Usage and documentation

#Example

hex, err := colors.ParseHEX("#fff")
rgb, err := colors.ParseRGB("rgb(0,0,0)")
rgb, err := colors.RGB(0,0,0)
rgba, err := colors.ParseRGBA("rgba(0,0,0,1)")
rgba, err := colors.RGBA(0,0,0,1)

// don't know which color, it was user selectable
color, err := colors.Parse("#000")

color.ToRGB()   // rgb(0,0,0)
color.ToRGBA()  // rgba(0,0,0,1)
color.ToHEX()   // #000000
color.IsLight() // false
color.IsDark()  // true

How to Contribute

Make a pull request...

License

Distributed under MIT License, please see license file in code for more details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBadColor is the default bad color error
	ErrBadColor = errors.New("parsing of color failed, Bad Color")
)

Functions

This section is empty.

Types

type Color

type Color interface {
	// ToHEX converts the Color interface to a concrete HEXColor
	ToHEX() *HEXColor

	// ToRGB converts the Color interface to a concrete RGBColor
	ToRGB() *RGBColor

	// ToRGBA converts the Color interface to a concrete RGBAColor
	ToRGBA() *RGBAColor

	// String returns the string representation of the Color
	String() string

	// IsLight returns whether the color is perceived to be a light color
	// http://stackoverflow.com/a/24213274/3158232 and http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
	IsLight() bool

	// IsDark returns whether the color is perceived to be a dark color
	//for perceived luminance, not strict math
	IsDark() bool

	// RGBA implements std-lib color.Color interface.
	// It returns the red, green, blue and alpha values for the color. Each value ranges within [0, 0xffff]
	RGBA() (r, g, b, a uint32)

	// Equal reports whether the colors are the same
	Equal(Color) bool
}

Color is the base color interface from which all others ascribe to

func Parse

func Parse(s string) (Color, error)

Parse parses an unknown color type to it's appropriate type, or returns a ErrBadColor

type HEXColor

type HEXColor struct {
	// contains filtered or unexported fields
}

HEXColor represents a HEX color

func ParseHEX

func ParseHEX(s string) (*HEXColor, error)

ParseHEX validates an parses the provided string into a HEXColor object

func (*HEXColor) Equal added in v1.3.0

func (c *HEXColor) Equal(d Color) bool

Equal reports whether c is the same color as d

func (*HEXColor) IsDark

func (c *HEXColor) IsDark() bool

IsDark returns whether the color is perceived to be a dark color

func (*HEXColor) IsLight

func (c *HEXColor) IsLight() bool

IsLight returns whether the color is perceived to be a light color

func (*HEXColor) RGBA added in v1.3.0

func (c *HEXColor) RGBA() (r, g, b, a uint32)

RGBA implements color.Color interface. It returns the red, green, blue and alpha values for the color. Each value ranges within [0, 0xffff]

func (*HEXColor) String

func (c *HEXColor) String() string

String returns the string representation on the HEXColor

func (*HEXColor) ToHEX

func (c *HEXColor) ToHEX() *HEXColor

ToHEX converts the HEXColor to a HEXColor it's here to satisfy the Color interface

func (*HEXColor) ToRGB

func (c *HEXColor) ToRGB() *RGBColor

ToRGB converts the HEXColor to and RGBColor

func (*HEXColor) ToRGBA

func (c *HEXColor) ToRGBA() *RGBAColor

ToRGBA converts the HEXColor to an RGBAColor

type RGBAColor

type RGBAColor struct {
	R uint8
	G uint8
	B uint8
	A float64
}

RGBAColor represents an RGBA color

func FromStdColor added in v1.2.0

func FromStdColor(c color.Color) *RGBAColor

func ParseRGBA

func ParseRGBA(s string) (*RGBAColor, error)

ParseRGBA validates an parses the provided string into an RGBAColor object supports both RGBA 255 and RGBA as percentages

func RGBA

func RGBA(r, g, b uint8, a float64) (*RGBAColor, error)

RGBA validates and returns a new RGBAColor object from the provided r, g, b, a values

func (*RGBAColor) Equal added in v1.3.0

func (c *RGBAColor) Equal(d Color) bool

Equal reports whether c is the same color as d

func (*RGBAColor) IsDark

func (c *RGBAColor) IsDark() bool

IsDark returns whether the color is perceived to be a dark color NOTE: this is determined only by the RGB values, if you need to take the alpha into account see the IsLightAlpha function

func (*RGBAColor) IsDarkAlpha

func (c *RGBAColor) IsDarkAlpha(bg Color) bool

IsDarkAlpha returns whether the color is perceived to be a dark color based on RGBA values and the provided background color algorithm based of of post here: http://stackoverflow.com/a/12228643/3158232

func (*RGBAColor) IsLight

func (c *RGBAColor) IsLight() bool

IsLight returns whether the color is perceived to be a light color NOTE: this is determined only by the RGB values, if you need to take the alpha into account see the IsLightAlpha function

func (*RGBAColor) IsLightAlpha

func (c *RGBAColor) IsLightAlpha(bg Color) bool

IsLightAlpha returns whether the color is perceived to be a light color based on RGBA values and the provided background color algorithm based of of post here: http://stackoverflow.com/a/12228643/3158232

func (*RGBAColor) RGBA added in v1.3.0

func (c *RGBAColor) RGBA() (r, g, b, a uint32)

RGBA implements color.Color interface. It returns the red, green, blue and alpha values for the color. Each value ranges within [0, 0xffff]

func (*RGBAColor) String

func (c *RGBAColor) String() string

String returns the string representation on the RGBAColor

func (*RGBAColor) ToHEX

func (c *RGBAColor) ToHEX() *HEXColor

ToHEX converts the RGBAColor to a HEXColor

func (*RGBAColor) ToRGB

func (c *RGBAColor) ToRGB() *RGBColor

ToRGB converts the RGBAColor to an RGBColor

func (*RGBAColor) ToRGBA

func (c *RGBAColor) ToRGBA() *RGBAColor

ToRGBA converts the RGBAColor to an RGBAColor it's here to satisfy the Color interface

type RGBColor

type RGBColor struct {
	R uint8
	G uint8
	B uint8
}

RGBColor represents an RGB color

func ParseRGB

func ParseRGB(s string) (*RGBColor, error)

ParseRGB validates an parses the provided string into an RGBColor object supports both RGB 255 and RGB as percentages

func RGB

func RGB(r, g, b uint8) (*RGBColor, error)

RGB validates and returns a new RGBColor object from the provided r, g, b values

func (*RGBColor) Equal added in v1.3.0

func (c *RGBColor) Equal(d Color) bool

Equal reports whether c is the same color as d

func (*RGBColor) IsDark

func (c *RGBColor) IsDark() bool

IsDark returns whether the color is perceived to be a dark color

func (*RGBColor) IsLight

func (c *RGBColor) IsLight() bool

IsLight returns whether the color is perceived to be a light color

func (*RGBColor) RGBA added in v1.3.0

func (c *RGBColor) RGBA() (r, g, b, a uint32)

RGBA implements color.Color interface. It returns the red, green, blue and alpha values for the color. Each value ranges within [0, 0xffff]

func (*RGBColor) String

func (c *RGBColor) String() string

String returns the string representation on the RGBColor

func (*RGBColor) ToHEX

func (c *RGBColor) ToHEX() *HEXColor

ToHEX converts the RGBColor to a HEXColor

func (*RGBColor) ToRGB

func (c *RGBColor) ToRGB() *RGBColor

ToRGB converts the RGBColor to an RGBColor it's here to satisfy the Color interface

func (*RGBColor) ToRGBA

func (c *RGBColor) ToRGBA() *RGBAColor

ToRGBA converts the RGBColor to an RGBAColor

Jump to

Keyboard shortcuts

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