Help us shape the future of Go! Take the 2026 Go Developer Survey

rgb

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2024 License: MIT Imports: 4 Imported by: 2

README

RGB color package in go

Package RGB provides a RGBA color type, which is a uint32, with a set of methods to manipulate the color.

RGB type is a 32bits color: 24bits for the RGB color itself and 8 bits for the alpha channel (transparency/opacity)

Go Reference

Usage

There is 4 ways to initialize a new RGBA color

// 1st way: directly with its 32bits exa value
blue1 := rgb.Color(0x0d6efdff)

// 2nd way: calling the NewRGB factory
blue2 := rgb.MakeRGB(13,110,253)

// 3rd way: calling the NewRGBA factory
blue3 := rgb.MakeRGBA(13,110,253,255)

// 4th way: calling the NewHexa factory
blue4 := rgb.ParseHexa("#0d6efdff")
if blue4 == nil {
    return errors.New("fail parsing the hexa color value")
}

When you call the NewHexa factory, possible string formats are:

  • '#A' means a color with R="0xAA", G="0xAA", B="0xAA", and a full opacity
  • '#ABC' means a color with R="0xAA", G="0xBB", B="0xCC", and a full opacity
  • '#ABCD' means a color with R="0xAA", G="0xBB", B="0xCC", and an opacity of "0xDD"
  • '#ABCDEF' means a color with R="0xAB", G="0xCD", B="0xEF", and a full opacity
  • '#ABCDEF88' means a color with R="0xAB", G="0xCD", B="0xEF", and an opacity of "0x88"

Then you can darken, lighten, convert to a grayscale, extracts the RGBA components or get the Hexa string representation of the color.

    str := rgb.NewRGB(10,10,10).Darken(0.8).Hexa()

Or if you which you can use predefined boostrap const color

    str := bootstrapcolor.Pink.Clone().Darken(0.8).Hexa()

Installing

go get -u github.com/larry868/rgb

Change Log

  • v1.1.0: migration to larry868 and go 1.23
  • v1.0.0: initial version

Licence

MIT

Documentation

Overview

Package rgb provides a RGBA color type, which is a uint32, with a set of methods to manipulate the color.

Color type is a 32bits color: 24bits for the RGB color itself and 8 bits for the alpha channel (transparency/opacity)

Index

Examples

Constants

View Source
const (
	Red     = Color(0xff0000ff)
	Green   = Color(0x00ff00ff)
	Blue    = Color(0x0000ffff)
	Yellow  = Color(0xffff00ff)
	Cyan    = Color(0x00ffffff)
	Magenta = Color(0xff00ffff)
	Black   = Color(0x000000ff)
	Silver  = Color(0xc0c0c0ff)
	Gray    = Color(0x808080ff)
	White   = Color(0xffffffff)
	None    = Color(0x00000000)
)

Basic RGB Colors

Variables

This section is empty.

Functions

This section is empty.

Types

type Color

type Color uint32

Color is a 32bits color: 24bits for the Color color itself and 8 bits for the alpha channel (transparency/opacity)

func MakeRGB

func MakeRGB(R uint8, G uint8, B uint8) Color

MakeRGB create a 24bits color with 100% opacity

func MakeRGBA

func MakeRGBA(R uint8, G uint8, B uint8, A uint8) Color

MakeRGBA create a 24bits RGB color with an Alpha channel for the opacity. Alpha varies from 0 (full transprency) to 255 (full opacity)

func ParseHexa

func ParseHexa(hex string) *Color

ParseHexa create a 24bits color with 100% opacity.

Accepted string formats:

the string can start with '#' but it s optional
'#A' means a color with R="0xAA", G="0xAA", B="0xAA", and a full opacity
'#ABC' means a color with R="0xAA", G="0xBB", B="0xCC", and a full opacity
'#ABCD' means a color with R="0xAA", G="0xBB", B="0xCC", and an opacity of "0xDD"
'#ABCDEF' means a color with R="0xAB", G="0xCD", B="0xEF", and a full opacity
'#ABCDEF88' means a color with R="0xAB", G="0xCD", B="0xEF", and an opacity of "0x88"

return nil if unable to convert the string in a valid color

func (Color) Alpha

func (c Color) Alpha() uint8

Returns the Alpha component of the color

func (Color) Blue

func (c Color) Blue() uint8

Returns the Blue component of the color

func (Color) Darken

func (c Color) Darken(factor float32) Color

Create a darker color, usefull to create a shade for example. The factor values is between 0 and 1. Alpha chanel stays unchanged.

A factor >= 1 returns an unchanged color
A factor <= 0 returns BLACK

func (Color) Green

func (c Color) Green() uint8

Returns the Green component of the color

func (Color) Hexa

func (c Color) Hexa() string

Hexa returns a 8 length char hexadecimal value of the color, starting with #

Example
blue := MakeRGB(13, 110, 253)
transparentblue := blue.Opacify(0.8)
fmt.Printf("blue=%s, transparent blue=%s\n", blue.Hexa(), transparentblue.Hexa())
Output:
blue=#0D6EFDFF, transparent blue=#0D6EFDCC

func (Color) Lighten

func (c Color) Lighten(factor float32) Color

Create a ligher color, a tint. The factor values is between 0 and 1. Alpha chanel stays unchanged.

A factor >= 1 returns WHITE
A factor <= 0 returns an unchanged color
Example
lightblue := MakeRGB(13, 110, 253).Lighten(0.5)
r, g, b := lightblue.RGB()
fmt.Printf("lightblue R=%d G=%d B=%d\n", r, g, b)
Output:
lightblue R=134 G=182 B=254

func (Color) Opacify

func (c Color) Opacify(opacityfactor float32) Color

Clone the color setting the alpha chanel with an opacity factor.

opacityfactor >= 1 set the alpha chanel to 255, fully opaque
opacityfactor <= 0 set the alpha chanel to 0, fully transparent

func (Color) RGB

func (c Color) RGB() (R uint8, G uint8, B uint8)

RGB returns the R, G, B components of the color

Example
blue := Color(0x0d6efdff)
r, g, b := blue.RGB()
fmt.Printf("blue R=%d G=%d B=%d\n", r, g, b)
Output:
blue R=13 G=110 B=253

func (Color) RGBA

func (c Color) RGBA() (R uint8, G uint8, B uint8, A uint8)

RGB returns the R, G, B components of the color and the value of the Alpha chanel

func (Color) Red

func (c Color) Red() uint8

Returns the Red component of the color

Directories

Path Synopsis
Package bootstrapcolor provides a set of default rgb constant colors.
Package bootstrapcolor provides a set of default rgb constant colors.

Jump to

Keyboard shortcuts

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