colorarty

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2016 License: MIT Imports: 5 Imported by: 0

README

colorarty

Go Report Card GoDoc

colorarty is a small library that analyses images and extracts a background, primary, secondary and detail colors, all suitable for reading.

Here's a simple example that converts extracted colors into CSS declarations:

package main

import (
	"fmt"
	"image"
	"image/color"
	_ "image/jpeg"
	"os"

	"github.com/victorgama/colorarty"
)

func main() {
	reader, _ := os.Open("/Users/victorgama/Downloads/sample.jpeg")
	img, _, _ := image.Decode(reader)
	result := colorarty.Analyse(img)
	fmt.Printf("#background { background: %s }\n", toCSS(*result.BackgroundColor))
	fmt.Printf(".primary { color: %s }\n", toCSS(*result.PrimaryColor))
	fmt.Printf(".secondary { color: %s }\n", toCSS(*result.SecondaryColor))
	fmt.Printf(".detail { color: %s }\n", toCSS(*result.DetailColor))
}

func toCSS(c color.Color) string {
	cr, cg, cb, _ := c.RGBA()
	r := float64(cr)
	g := float64(cg)
	b := float64(cb)
	r /= 0x101
	g /= 0x101
	b /= 0x101
	return fmt.Sprintf("rgba(%.0f, %.0f, %.0f, 1)", r, g, b)
}

The generated CSS was used to build this next example:

example

Installing

  1. Download and install it:
$ go get -u github.com/victorgama/colorarty
  1. Import it in your code:
import "github.com/victorgama/colorarty"

License

colorarty was inspired by this post and this project, both from Panic, Inc. 💖🦄

MIT License

Copyright (c) 2016 Victor Gama

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Result

type Result struct {
	BackgroundColor *color.Color
	PrimaryColor    *color.Color
	SecondaryColor  *color.Color
	DetailColor     *color.Color
}

Result contains the extracted picture colors for the input image

func Analyse

func Analyse(img image.Image) *Result

Analyse performs several operations on a given image in order to extract background, primary, secondary and detail colors for it.

Jump to

Keyboard shortcuts

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