Documentation
¶
Overview ¶
Package yac helps to analyze icons and give out up to 4 colors (for example for background, font and button of app card) based and complementary icons colors.
Initial Implementation ¶
The color for the background of the card was selected automatically based on the icon, the button - translucent white. The algorithm tried to determine the main color of the icon, sorting the pixels by hue. Such an approach did not always give a beautiful result, it had shortcomings:
wrong definition of color, "dirty" colors due to averaging, dim buttons, boring cards.
What really wanted to ¶
The card was to be a real continuation of the icon. The colors are juicy and bright. We wanted to create the feeling that the card was carefully done by hand, and not slipped something carelessly generated automatically.
We want to do more beautifully always, but the resources are not unlimited. To allocate a command to write a miracle library by definition of colors was not planned. So, the task:
Minimal forces to improve the algorithm for determining colors, to figure out how to paint a card beautifully, without inventing a spaceship.
New algorithm for determining colors ¶
Step 1 ¶
Take the icon. We discard white, black and transparent pixels.
Step 2 ¶
Reduce the resulting image to a size of 2 × 2 pixels (with anti-aliasing disabled). As a result, we get four colors for the icon. If the original picture is homogeneous, they can be repeated - it's okay.
We have disabled anti-aliasing, so that colors do not mix, do not become "dirty".
In fact, it turns out like this: the square is divided into four parts, we take the average pixel from the top row of each quarter. In the implementation of everything is simple: we do not even need a real downsample image and generally work with graphics. Pixels with the desired position are taken from a one-dimensional array obtained after the first step.
Step 3 ¶
Almost everything is ready. Remained quite a bit: get the resulting colors, translate to HSL, sort by lightness (L). We are painting a card.
Light scheme:
background - the lightest color; button - closest to the light; text - the darkest.
Dark scheme (if two or more colors are dark):
background - the darkest color; button - closest to the dark one; text - the lightest.
Applying colors, check the contrast: Lightness difference between background and button ≥ 20; between the background and the text ≥ 60. If not, correct.
Result ¶
We have got colorful cards, from real colors of icons, without "dirty" impurities. Due to the use of several colors, the card looks much livelier. It is especially pleasant that with a homogeneous icon background the card becomes its direct continuation: the border between them is not noticeable at all.
And most importantly: we provided for special cases:
Icon from the same color: make the background a little darker so that it does not merge. Icon with background: look at pixels around the edges; if everyone is the same, we put the same background of the card.
Index ¶
- Variables
- func Analyze(data interface{}) ([]color.Color, error)
- func Filter(src image.Image) []color.Color
- func Find(c ...color.Color) []color.Color
- func Fix(colors ...color.Color) []color.Color
- func IsDarkScheme(colors ...color.Color) bool
- func IsLightScheme(colors ...color.Color) bool
- func Open(src interface{}) (img image.Image, err error)
- func Prune(colors ...color.Color) []color.Color
- func Sort(colors ...color.Color) []color.Color
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupportedType = errors.New("use string, byte array or io.Reader only")
ErrUnsupportedType is returned in case if raw input data of image is not supported for decoding by Open method.
Functions ¶
func Analyze ¶
Analyze method grab your raw input image data and analyze them on key colors. This method do all basic steps: Open > Filter > Find > Fix and Sort. Returned an sorted color.Color array of 4 keys colors which can already be used as a basis for coloring.
func Filter ¶
Filter method filter raw image pixels from white, black and any-transparent pixels. Returned color.Color array of filtered pixels.
func Find ¶ added in v1.1.0
Find method find key pixels in color.Color array of filtered (or not) pixels. Returned an non-sorted color.Color array of 4 key colors which can already be used as a basis for coloring.
func Fix ¶ added in v1.1.0
Fix helper check first, second and last colors and fixed his contrast by lightness for better visual result. Returned fixed array of color.Color.
func IsDarkScheme ¶ added in v1.1.0
IsDarkScheme helper check first color.Color array element of sorted key colors on lightness. Returned true if current array contains colors for dark scheme.
func IsLightScheme ¶ added in v1.1.0
IsLightScheme helper check first color.Color array element of sorted key colors on lightness. Returned true if current array contains colors for light scheme.
func Prune ¶ added in v1.1.0
Prune helper check Colors array on duplicated items. Returned Colors array with unique color.Color items in unsorted order.
Types ¶
This section is empty.