colors

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2018 License: MIT Imports: 6 Imported by: 0

README

Package colors

Build 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 gopkg.in/go-playground/colors.v1

or to update

go get -u gopkg.in/go-playground/colors.v1

Then import the validator package into your own code.

import "gopkg.in/go-playground/colors.v1"

Usage and documentation

Please see http://godoc.org/gopkg.in/go-playground/colors.v1 for detailed usage docs.

#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

There will always be a development branch for each version i.e. v1-development. In order to contribute, please make your pull requests against those branches.

If the changes being proposed or requested are breaking changes, please create an issue, for discussion or create a pull request against the highest development branch for example this package has a v1 and v1-development branch however, there will also be a v2-development brach even though v2 doesn't exist yet.

I am not a color expert by any means and am sure that there could be better or even more efficient ways to accomplish the color conversion and so forth and I welcome any suggestions or pull request to help!

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() *HEXColor
	ToRGB() *RGBColor
	ToRGBA() *RGBAColor
	String() string
	IsLight() bool // http://stackoverflow.com/a/24213274/3158232 and http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
	IsDark() bool  //for perceived luminance, not strict math
}

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) 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) 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 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) 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) 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) 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) 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