plates

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2023 License: BSD-3-Clause Imports: 14 Imported by: 0

README

Plates Build Go Report Card License

Package for dealing with RGB, HSV and HSL colors, mixing colors and for reading and writing images.

Plates comes with the cmd/puppyart utility.

Puppyart

Quick installation
go install github.com/xyproto/plates/cmd/puppyart@latest
Original image

puppy

The above image is by Andrea Schaffer and is licensed under cc-by-2.0.

Transformed image

puppy

Example code

The transformed image was generated with the following program:

package main

import (
    "image"
    "image/color"
    "image/draw"
    "log"

    "github.com/xyproto/plates"
)

// convert takes an image file and modifies it based on a specified threshold and two given colors.
// It returns a processed image.
func convert(infilename string, thresh uint8, color1, color2 color.RGBA) image.Image {
    // Combine the two colors to create a mixed color
    mixcolor := plates.PaintMix(color1, color2)

    // Load the input image
    img, err := plates.Read(infilename)
    if err != nil {
        log.Fatalln(err)
    }

    // Separate the image into three based on the specified threshold and intensity: color1, mixcolor, and color2.
    color1image, mixcolorimage, color2image := plates.Separate3(img, color1, mixcolor, color2, thresh)

    // Combine the color1 image and mixcolor image to create a new image plate with color1
    color1plate := plates.AddToAs(color1image, mixcolorimage, color1)

    // Combine the color1plate and color2image to create an allplate image with color2 color
    // And then combine the resulting image with the mixcolorimage to get the final output image.
    allplate := plates.AddToAs(color1plate, color2image, color2)
    allplate = plates.AddToAs(allplate, mixcolorimage, mixcolor)

    return allplate
}

func main() {
    // Define input colors and process the image using convert function.
    // Repeat this process for different color combinations.
    image1 := convert("puppy.png", 255, color.RGBA{0, 0, 255, 255}, color.RGBA{255, 255, 255, 255})
    image2 := convert("puppy.png", 255, color.RGBA{16, 63, 255, 255}, color.RGBA{0, 0, 0, 255})
    image3 := convert("puppy.png", 255, color.RGBA{255, 0, 0, 255}, color.RGBA{255, 255, 0, 255})
    image4 := convert("puppy.png", 255, color.RGBA{16, 255, 255, 255}, color.RGBA{255, 0, 255, 255})

    // Create a new image to contain the 4 processed images.
    width := image1.Bounds().Dx() * 2
    height := image2.Bounds().Dy() * 2
    newimage := image.NewRGBA(image.Rect(0, 0, width, height))

    // Arrange the 4 processed images on the new image.
    draw.Draw(newimage, image1.Bounds(), image1, newimage.Bounds().Min, draw.Src)
    draw.Draw(newimage, image.Rect(image1.Bounds().Dx(), 0, width, image1.Bounds().Dy()), image2, newimage.Bounds().Min, draw.Src)
    draw.Draw(newimage, image.Rect(0, image1.Bounds().Dy(), image1.Bounds().Dx(), height), image3, newimage.Bounds().Min, draw.Src)
    draw.Draw(newimage, image.Rect(image1.Bounds().Dx(), image1.Bounds().Dy(), width, height), image4, newimage.Bounds().Min, draw.Src)

    // Write the final image to file.
    if err := plates.Write("generated.png", newimage); err != nil {
        log.Fatalln(err)
    }
}

General information

Documentation

Overview

Package plates provides utilities for manipulating colors and images.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddToAs

func AddToAs(orig image.Image, addimage image.Image, addcolor color.RGBA) image.Image

AddToAs will take an image, add the nontransparent colors from addimage, use addcolor and return an image.

func Blue

func Blue(m image.Image) image.Image

Blue function isolates and returns the blue channel from an image. It returns a new image where only the blue component of each pixel's color is retained.

func CloseTo1

func CloseTo1(m image.Image, target color.RGBA, threshold uint8) image.Image

CloseTo1 function isolates pixels in an image that are similar to a target color within a given threshold. It returns a new image where only the pixels close to the target color are retained. Pixels that do not meet the threshold will be set to black (RGB{0,0,0}).

func CloseTo2

func CloseTo2(m image.Image, target color.RGBA, threshold uint8) image.Image

CloseTo2 will pick out only the colors close to the given color, within a given threshold. Make it uniform. Zero alpha to unused pixels in returned image.

func Green

func Green(m image.Image) image.Image

Green function isolates and returns the green channel from an image. It returns a new image where only the green component of each pixel's color is retained.

func HLS

func HLS(r, g, b float64) (float64, float64, float64)

HLS will convert an RGB color to hue, lightness and saturation

func HLStoRGB

func HLStoRGB(h, l, s float64) (float64, float64, float64)

HLStoRGB will convert a HLS color to red, green, blue

func HSV

func HSV(cr color.RGBA) (uint8, uint8, uint8)

HSV will convert an RGB color to huse, saturation and value

func Hue

func Hue(cr color.RGBA) float64

Hue will convert an RGB color to a Hue float

func PaintMix

func PaintMix(c1, c2 color.RGBA) color.RGBA

PaintMix will attempt to mix two RGB colors, a bit like how paint mixes (but not exactly like it)

func Read

func Read(filename string) (image.Image, error)

Read tries to read the given image filename and return an image.Image The supported extensions are: .png, .jpg, .jpeg, .gif, .ico, .bmp and .webp

func Red

func Red(m image.Image) image.Image

Red function isolates and returns the red channel from an image. It returns a new image where only the red component of each pixel's color is retained.

func Separate3

func Separate3(inImage image.Image, color1, color2, color3 color.RGBA, threshold uint8) (image.Image, image.Image, image.Image)

Separate3 an image into three images with the three given colors and a given threshold.

func Write

func Write(filename string, img image.Image) error

Write tries to write then given image.Image to a file. The supported extensions are: .png, .jpg, .jpeg. .gif, .ico, .bmp, .webp and .xpm

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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