Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Go package to generate and manage color palettes & schemes 🎨

License

NotificationsYou must be signed in to change notification settings

muesli/gamut

Repository files navigation

Latest ReleaseBuild StatusCoverage StatusGo ReportCardGoDoc

Go package to generate and manage color palettes & schemes

import"github.com/muesli/gamut"import"github.com/muesli/gamut/palette"import"github.com/muesli/gamut/theme"

Colors

gamut operates on various color spaces internally, but all color values you passin as parameters and all return values will match Go’s color.Color interface.

Let’s start with the basics. Just for convenience there’s a hex-value parser:

color=gamut.Hex("#333")color=gamut.Hex("#ABCDEF")

Both the short and standard formats are supported.

Conversely you can retrieve the hex encoding of anycolor.Color value:

hex=gamut.ToHex(color)

Around the Color Wheel

TheDarker andLighter functions darken and lighten respectively a givencolor value by a specified percentage, without changing the color's hue:

// returns a 10% darker version of colorcolor=gamut.Darker(color,0.1)// returns a 30% lighter version of colorcolor=gamut.Lighter(color,0.3)

Complementary returns the complementary color for a given color:

color=gamut.Complementary(color)

Contrast returns the color with the highest contrast to a given color, eitherblack or white:

color=gamut.Contrast(color)

To retrieve a color with the same lightness and saturation, but a differentangle on the color wheel, you can use the HueOffset function:

color=gamut.HueOffset(color,90)

You can also go in the opposite direction by using negative values.

Schemes

All the following functions return colors of a different hue, but with the samelightness and saturation as the given colors:

Triadic schemes are made up of three hues equally spaced around the color wheel:

colors=gamut.Triadic(color)

Quadratic schemes are made up of four hues equally spaced around the color wheel:

colors=gamut.Quadratic(color)

Tetradic schemes are made up by two colors and their complementary values:

colors=gamut.Tetradic(color1,color2)

Analogous schemes are created by using colors that are next to each other on thecolor wheel:

colors=gamut.Analogous(color)

SplitComplementary schemes are created by using colors next to the complementaryvalue of a given color:

colors=gamut.SplitComplementary(color)

Warm/Cool Colors

ok=gamut.Warm(color)ok=gamut.Cool(color)

Shades, Tints & Tones

Monochromatic returns colors of the same hue, but with a differentsaturation/lightness:

colors=gamut.Monochromatic(color,8)

Monochromatic Palette

Shades returns colors blended from the given color to black:

colors=gamut.Shades(color,8)

Shades Palette

Tints returns colors blended from the given color to white:

colors=gamut.Tints(color,8)

Tints Palette

Tones returns colors blended from the given color to gray:

colors=gamut.Tones(color,8)

Tones Palette

Blending Colors

Blends returns interpolated colors by blending two colors:

colors=gamut.Blends(color1,color2,8)

Blends Palette

Palettes

Gamut comes with six curated color palettes: Wikipedia, Crayola, CSS, RAL,Resene, and Monokai. The Wikipedia palette is an import of common colors fromWikipedia’s List of Colors. New curated palettes and importers are welcome. Send mea pull request!

NameColorsSource
Wikipedia1609https://en.wikipedia.org/wiki/List_of_colors_(compact)
Crayola180https://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors
CSS147https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
RAL213https://en.wikipedia.org/wiki/List_of_RAL_colors
Resene759http://www.resene.co.nz
Monokai17

The function Colors lets you retrieve all colors in a palette:

for_,c:=rangepalette.Wikipedia.Colors() {fmt.Println(c.Name,c.Color)}

This will print out a list of 1609 color names, as defined by Wikipedia.

Creating Your Own Palettes

varp gamut.Palettep.AddColors(    gamut.Colors{        {"Name",gamut.Hex("#123456"),"Reference"},...    })

Name and Reference are optional when creating your own palettes.

Names

Each color in the curated palettes comes with an “official” name. You can filterpalettes by colors with specific names. This code snippet will return a list ofall “blue” colors in the Wikipedia palette:

colors=palette.Wikipedia.Filter("blue")

You can access a color with a specific name using theColor function:

color,ok=palette.Wikipedia.Color("Pastel blue")

Calling a palette’sName function with a given color returns the name & distanceof the closest (perceptually) matching color in it:

name,distance=palette.Wikipedia.Name(color)// name = "Baby blue"// distance between 0.0 and 1.0

Mixing Palettes

You can combine all colors of two palettes by mixing them:

p=palette.Crayola.MixedWith(palette.Monokai)

Perception

Sometimes you got a slice of colors, but you have a limited color palette towork with. The Clamped function returns a slice of the closest perceptuallymatching colors in a palette, maintaining the same order as the original sliceyou provided. Finally you can remix your favorite wallpapers in Crayola-style!

colors=palette.Crayola.Clamped(colors)

Generating Color Palettes

Color Generators, like the providedPastelGenerator,WarmGenerator orHappyGenerator can produce random (within the color space constraints of thegenerator) color palettes:

colors,err=gamut.Generate(8, gamut.PastelGenerator{})

Pastel Palette

TheSimilarHueGenerator produces colors with a hue similar to a given color:

colors,err=gamut.Generate(8, gamut.SimilarHueGenerator{Color:gamut.Hex("#2F1B82")})

Similar Hue Palette

Using theColorGenerator interface, you can also write your own color generators:

typeBrightGeneratorstruct {BroadGranularity}func (ccBrightGenerator)Valid(col colorful.Color)bool {_,_,l:=col.Lab()return0.7<=l&&l<=1.0}...colors,err:=gamut.Generate(8,BrightGenerator{})

Only colors with a lightness between 0.7 and 1.0 will be accepted by this generator.

Themes

NameColors
Monokai7

Roles

color=theme.MonokaiTheme.Role(theme.Foreground)

Available roles areForeground,Background,Base,AlternateBase,Text,Selection,Highlight.

Feedback

Got some feedback or suggestions? Please open an issue or drop me a note!


[8]ページ先頭

©2009-2025 Movatter.jp