Documentation
¶
Overview ¶
Package clr provides efficient 16-bit and 32-bit color representation types and conversion between hexadecimal, RGB, RGBA and CSS named-colors. Along with W3C named-color hex value constants.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalid = errors.New("invalid value: expected a color name or 3, 4, 6, or 8 hexadecimal digits") ErrExceedRange = errors.New("expected an integer between 0 and 255") Hex3 = regexp.MustCompile(`^#?[[:xdigit:]]{3}$`) Hex4 = regexp.MustCompile(`^#?[[:xdigit:]]{4}$`) Hex6 = regexp.MustCompile(`^#?[[:xdigit:]]{6}$`) Hex8 = regexp.MustCompile(`^#?[[:xdigit:]]{8}$`) RGB = regexp.MustCompile(`^rgb\(\d{1,3},\d{1,3},\d{1,3}\)$`) RGBA = regexp.MustCompile(`^rgba\(\d{1,3},\d{1,3},\d{1,3},\d{1,3}\)$`) )
Functions ¶
func IsValidHex3 ¶
func IsValidHex4 ¶
func IsValidHex6 ¶
func IsValidHex8 ¶
func IsValidRGB ¶
func IsValidRGBA ¶
func TrimRGBPrefix ¶
Types ¶
type C16 ¶
type C16 uint16
C16 represents a 16-bit color with 4 channels (red, green, blue and alpha) using 4-bits each (0-15 or 0-F).
func FromHex3 ¶
FromHex3 converts a 3-digit hexadecimal string into a 16-bit color.
FromHex3("#F60")
func FromHex4 ¶
FromHex4 converts a 4-digit hexadecimal string into a 16-bit color.
FromHex4("#F60F")
func New16 ¶
New16 returns a new 16-bit color where R, G, B, and A are converted from 0-255 range to 0-F hexadecimal. Use New16Hex for 4-bit hexadecimal integers ranging from 0 to F.
New16Hex(255, 102, 0, 255)
func New16Hex ¶
New16Hex returns a new 16-bit color where R, G, B, and A are expected to be a hexadecimal ranging from 0 to F. Use New16 for 8-bit integers ranging from 0 to 255.
New16Hex(0xF, 0x6, 0x0, 0xF)
func New16Str ¶
New16Str parses string color and if valid returns a 16-bit color. Accepts any 3, 4, 6 or 8 digit hexadecimal, CSS named-color, RGB or RGBA color string.
FromHex4("rgba(255,102,0,255)")
func Round ¶
Round converts a single C32 channel into a single C16 channel. The returned C16 channel is rounded to the closest hexadecimal integer for optimal color accuracy.
func (C16) Hex3 ¶
Hex3 returns a 3-digit hexadecimal color string without the alpha channel like `#f00`.
func (C16) Hex4 ¶
Hex4 returns a 4-digit hexadecimal color string with the alpha channel like `#f00f`.
func (C16) Hex6 ¶
Hex6 returns a 6-digit hexadecimal color string with the alpha channel like `#ff0000`.
func (C16) Hex8 ¶
Hex8 returns an 8-digit hexadecimal color string with the alpha channel like `#ff0000ff`.
func (C16) Lossy ¶
Lossy returns the shortest possible string representation of color ignoring the alpha channel. Returns:
"red" otherwise, a 3-digit hexadecimal color string (like `#f60`).
func (C16) RGBA ¶
RGBA returns an RGBA color string with the alpha channel like `rgba(255,0,0,255)`.
func (C16) String ¶
String returns the shortest possible string representation of color without loosing any color accuracy. Returns:
"red" or a 4-digit hexadecimal color string when the alpha channel is <= 254 (like `#f608`) otherwise, a 3-digit hexadecimal color string when the alpha channel equals 255 (like `#f60`).
type C32 ¶
type C32 uint32
C32 represents a 32-bit color with four channels (red, green, blue and alpha) using 8-bits each (0-255 or 0-FF).
const ( AliceBlue C32 = 0xf0f8ffff AntiqueWhite C32 = 0xfaebd7ff Aqua C32 = 0x00ffffff Aquamarine C32 = 0x7fffd4ff Azure C32 = 0xf0ffffff Beige C32 = 0xf5f5dcff Bisque C32 = 0xffe4c4ff Black C32 = 0x000000ff BlanchedAlmond C32 = 0xffebcdff Blue C32 = 0x0000ffff BlueViolet C32 = 0x8a2be2ff Brown C32 = 0xa52a2aff BurlyWood C32 = 0xdeb887ff CadetBlue C32 = 0x5f9ea0ff Chartreuse C32 = 0x7fff00ff Chocolate C32 = 0xd2691eff Coral C32 = 0xff7f50ff CornflowerBlue C32 = 0x6495edff Cornsilk C32 = 0xfff8dcff Crimson C32 = 0xdc143cff DarkBlue C32 = 0x00008bff DarkCyan C32 = 0x008b8bff DarkGoldenrod C32 = 0xb8860bff DarkGray C32 = 0xa9a9a9ff DarkGreen C32 = 0x006400ff DarkKhaki C32 = 0xbdb76bff DarkMagenta C32 = 0x8b008bff DarkOliveGreen C32 = 0x556b2fff DarkOrange C32 = 0xff8c00ff DarkOrchid C32 = 0x9932ccff DarkRed C32 = 0x8b0000ff DarkSalmon C32 = 0xe9967aff DarkSeaGreen C32 = 0x8fbc8fff DarkSlateBlue C32 = 0x483d8bff DarkSlateGray C32 = 0x2f4f4fff DarkTurquoise C32 = 0x00ced1ff DarkViolet C32 = 0x9400d3ff DeepPink C32 = 0xff1493ff DeepSkyBlue C32 = 0x00bfffff DimGray C32 = 0x696969ff DodgerBlue C32 = 0x1e90ffff Firebrick C32 = 0xb22222ff FloralWhite C32 = 0xfffaf0ff ForestGreen C32 = 0x228b22ff Fuchsia C32 = 0xff00ffff Gainsboro C32 = 0xdcdcdcff GhostWhite C32 = 0xf8f8ffff Gold C32 = 0xffd700ff Goldenrod C32 = 0xdaa520ff Gray C32 = 0x808080ff Green C32 = 0x008000ff GreenYellow C32 = 0xadff2fff Honeydew C32 = 0xf0fff0ff HotPink C32 = 0xff69b4ff IndianRed C32 = 0xcd5c5cff Indigo C32 = 0x4b0082ff Ivory C32 = 0xfffff0ff Khaki C32 = 0xf0e68cff Lavender C32 = 0xe6e6faff LavenderBlush C32 = 0xfff0f5ff LawnGreen C32 = 0x7cfc00ff LemonChiffon C32 = 0xfffacdff LightBlue C32 = 0xadd8e6ff LightCoral C32 = 0xf08080ff LightCyan C32 = 0xe0ffffff LightGoldenrodYellow C32 = 0xfafad2ff LightGray C32 = 0xd3d3d3ff LightGreen C32 = 0x90ee90ff LightPink C32 = 0xffb6c1ff LightSalmon C32 = 0xffa07aff LightSeaGreen C32 = 0x20b2aaff LightSkyBlue C32 = 0x87cefaff LightSlateGray C32 = 0x778899ff LightSteelBlue C32 = 0xb0c4deff LightYellow C32 = 0xffffe0ff Lime C32 = 0x00ff00ff LimeGreen C32 = 0x32cd32ff Linen C32 = 0xfaf0e6ff Maroon C32 = 0x800000ff MediumAquamarine C32 = 0x66cdaaff MediumBlue C32 = 0x0000cdff MediumOrchid C32 = 0xba55d3ff MediumPurple C32 = 0x9370dbff MediumSeaGreen C32 = 0x3cb371ff MediumSlateBlue C32 = 0x7b68eeff MediumSpringGreen C32 = 0x00fa9aff MediumTurquoise C32 = 0x48d1ccff MediumVioletRed C32 = 0xc71585ff MidnightBlue C32 = 0x191970ff MintCream C32 = 0xf5fffaff MistyRose C32 = 0xffe4e1ff Moccasin C32 = 0xffe4b5ff OldLace C32 = 0xfdf5e6ff Olive C32 = 0x808000ff OliveDrab C32 = 0x6b8e23ff Orange C32 = 0xffa500ff OrangeRed C32 = 0xff4500ff Orchid C32 = 0xda70d6ff PaleGoldenrod C32 = 0xeee8aaff PaleGreen C32 = 0x98fb98ff PaleTurquoise C32 = 0xafeeeeff PaleVioletRed C32 = 0xdb7093ff PapayaWhip C32 = 0xffefd5ff PeachPuff C32 = 0xffdab9ff Peru C32 = 0xcd853fff Pink C32 = 0xffc0cbff Plum C32 = 0xdda0ddff PowderBlue C32 = 0xb0e0e6ff Purple C32 = 0x800080ff RebeccaPurple C32 = 0x663399ff Red C32 = 0xff0000ff RosyBrown C32 = 0xbc8f8fff RoyalBlue C32 = 0x4169e1ff SaddleBrown C32 = 0x8b4513ff Salmon C32 = 0xfa8072ff SandyBrown C32 = 0xf4a460ff SeaGreen C32 = 0x2e8b57ff Seashell C32 = 0xfff5eeff Sienna C32 = 0xa0522dff Silver C32 = 0xc0c0c0ff SkyBlue C32 = 0x87ceebff SlateBlue C32 = 0x6a5acdff SlateGray C32 = 0x708090ff Snow C32 = 0xfffafaff SpringGreen C32 = 0x00ff7fff SteelBlue C32 = 0x4682b4ff Tan C32 = 0xd2b48cff Teal C32 = 0x008080ff Thistle C32 = 0xd8bfd8ff Tomato C32 = 0xff6347ff Turquoise C32 = 0x40e0d0ff Violet C32 = 0xee82eeff Wheat C32 = 0xf5deb3ff White C32 = 0xffffffff WhiteSmoke C32 = 0xf5f5f5ff Yellow C32 = 0xffff00ff YellowGreen C32 = 0x9acd32ff Cyan = Aqua Magenta = Fuchsia )
func FromHex6 ¶
FromHex6 converts a 6-digit hexadecimal string into a 32-bit color.
FromHex6("#FF6600")
func FromHex8 ¶
FromHex8 converts an 8-digit hexadecimal string into a 32-bit color.
FromHex8("#FF6600FF")
func FromName ¶
FromName converts a color-name string to its 32-bit color representation. Any color with an undefined name returns zero (0).
func FromRGB ¶
FromRGB converts 3 (RGB) or 4 (RGBA) comma separated channels ranging from 0 to 255 in base 10 into C32. Expected color string formats are:
rgb(0,0,0) rgba(0, 0, 0, 0) 0,0,0 0,0,0,0
func (C32) Hex3 ¶
Hex3 returns a lossy 3-digit hexadecimal color string without the alpha channel like `#f00`.
func (C32) Hex4 ¶
Hex4 returns a lossy 4-digit hexadecimal color string with the alpha channel like `#f00f`.
func (C32) Hex6 ¶
Hex6 returns a 6-digit hexadecimal color string with the alpha channel like `#ff0000`.
func (C32) Hex8 ¶
Hex8 returns an 8-digit hexadecimal color string with the alpha channel like `#ff0000ff`.
func (C32) Is24bit ¶
Is24bit checks if a color's red, green and blue channels can be represented as a 16-bit color without any color accuracy loss.