blend

package module
v0.0.0-...-f26b6cf Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2021 License: MIT Imports: 4 Imported by: 1

README

blend

Image processing library and rendering toolkit for Go. (WIP)

Installation:

This library is compatible with Go1.

go get github.com/phrozen/blend

Usage:

import "github.com/phrozen/blend"

Use this convenience function:

func BlendNewImage(dst, src image.Image, mode BlendFunc) image.Image {
  ...
}
// src is the top layer, dst is the bottom layer.

For example:

import "github.com/phrozen/blend"

// Read two images 'source' and 'destination' (image.Image)

// Blend source (top layer) into destination (bottom layer)
// using Color Burn blending mode.
img1 := blend.BlendNewImage(destination, source, blend.ColorBurn)


// Save img or blend it again applying another blend mode.
img2 := blend.BlendNewImage(img1, source, blend.Screen)

If you want to apply the Blend Mode to an image and modify it without returning a copy, you must provide a mutable image type, one that implements 'draw.Image' interface. Use this function.

func BlendImage(dst draw.Image, src image.Image, mode BlendFunc) {
  ...
}
// src is the top layer, dst is the bottom layer and image that will be applied to.

This function is faster as it does not copy the contents of the original image and applies the Blend Mode just to the intersection of both layers. Most images returned by the encoders of the standard library are already mutable as they implement the 'draw.Image' interface, but you will have to apply and interface/type assertion first.

(Note: jpeg decoder returns color images in YCbCr color mode that does not implement 'draw.Image', PNG decoder returns mostly RGBA family types and should work)

import "github.com/phrozen/blend"

// Read two images 'source' and 'destination' (image.Image)

dst, ok := destination.(draw.Image)
if ok {
  blend.BlendImage(dst, source, blend.ColorBurn)
  blend.BlendImage(dst, source, blend.Screen)
}

The package an be easily extended as it uses the standard library interfaces from 'image', 'image/draw' and 'image/color'.

type BlendFunc func(dst, src color.Color) color.Color

A BlendFunc is applied to each color (RGBA) of an image (although included blend modes does not use the Alpha channel atm). Just create your own BlendFunc to add custom functionality.

The library uses float64 internally for precision, math operations, and conversions to the 'image' interfaces.

At the moment it supports the following blending modes:
  • Darken
  • Multiply
  • Color Burn
  • Linear Burn
  • Darker Color

  • Lighten
  • Screen
  • Color Dodge
  • Linear Dodge
  • Lighter Color

  • Overlay
  • Soft Light
  • Hard Light
  • Vivid Light
  • Linear Light
  • Pin Light
  • Hard Mix

  • Difference
  • Exclusion
  • Substract
  • Divide

  • Hue
  • Saturation
  • Color
  • Luminosity

  • Add
  • Reflex
  • Phoenix

Notes:

  • Add, Reflex, and Phoenix modes are not in PSD.
  • Vivid Light produces different results than PSD, affects Hard Mix issue #2
  • Saturation, Color, and Luminosity modes produce different results than PSD, but the results are either identical to The GIMP or pretty similar. issue #3

Check the examples directory for more on blending modes.

More features to come.

License:

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.

MIT License

Documentation

Overview

Package blend implements blending mode functions bewteen images, and some utility functions for image processing.

The fundamental part of the library is the type BlendFunc, the function is applied to each pixel where the top layer (src) overlaps the bottom layer (dst) of both given 'image' interfaces.

This library provides many of the widely used blending functions to be used either as 'mode' parameter to the Blend() primary function, or to be used individually providing two 'color' interfaces. You can implement your own blending modes and pass them into the Blend() function.

This is the list of the currently implemented blending modes:

Add, Color, Color Burn, Color Dodge, Darken, Darker Color, Difference, Divide, Exclusion, Hard Light, Hard Mix, Hue, Lighten, Lighter Color, Linear Burn, Linear Dodge, Linear Light, Luminosity, Multiply, Overlay, Phoenix, Pin Light, Reflex, Saturation, Screen, Soft Light, Substract, Vivid Light.

Check github for more details: http://github.com/phrozen/blend

Index

Constants

This section is empty.

Variables

View Source
var Modes = map[string]BlendFunc{
	"add":           Add,
	"color":         Color,
	"color_burn":    ColorBurn,
	"color_dodge":   ColorDodge,
	"darken":        Darken,
	"darker_color":  DarkerColor,
	"difference":    Difference,
	"divide":        Divide,
	"exclusion":     Exclusion,
	"hard_light":    HardLight,
	"hard_mix":      HardMix,
	"hue":           Hue,
	"lighten":       Lighten,
	"lighter_color": LighterColor,
	"linear_burn":   LinearBurn,
	"linear_dodge":  LinearDodge,
	"linear_light":  LinearLight,
	"luminosity":    Luminosity,
	"multiply":      Multiply,
	"overlay":       Overlay,
	"phoenix":       Phoenix,
	"pin_light":     PinLight,
	"reflex":        Reflex,
	"saturation":    Saturation,
	"screen":        Screen,
	"soft_light":    SoftLight,
	"substract":     Substract,
	"vivid_light":   VividLight,
}

Available Moodes map

Functions

func Add

func Add(dst, src color.Color) color.Color

Add ...

func BlendImage

func BlendImage(dst draw.Image, src image.Image, mode BlendFunc)

BlendImage blends src image (top layer) into dst image (bottom layer) using the BlendFunc provided by mode. BlendFunc is applied to each pixel where the src image overlaps the dst image and the result is stored in the original dst image, src image is unmutable.

func BlendNewImage

func BlendNewImage(dst, src image.Image, mode BlendFunc) image.Image

BlendNewImage blends src image (top layer) into dst image (bottom layer) using the BlendFunc provided by mode. BlendFunc is applied to each pixel where the src image overlaps the dst image and returns the resulting image without modifying src, or dst as they are both unmutable.

func Color

func Color(dst, src color.Color) color.Color

Color ...

func ColorBurn

func ColorBurn(dst, src color.Color) color.Color

ColorBurn ...

func ColorDodge

func ColorDodge(dst, src color.Color) color.Color

ColorDodge ...

func Darken

func Darken(dst, src color.Color) color.Color

Darken ...

func DarkerColor

func DarkerColor(dst, src color.Color) color.Color

DarkerColor ...

func Difference

func Difference(dst, src color.Color) color.Color

Difference ...

func Divide

func Divide(dst, src color.Color) color.Color

Divide ...

func Exclusion

func Exclusion(dst, src color.Color) color.Color

Exclusion ...

func HardLight

func HardLight(dst, src color.Color) color.Color

HardLight ...

func HardMix

func HardMix(dst, src color.Color) color.Color

HardMix ...

func Hue

func Hue(dst, src color.Color) color.Color

Hue ...

func Lighten

func Lighten(dst, src color.Color) color.Color

Lighten ...

func LighterColor

func LighterColor(dst, src color.Color) color.Color

LighterColor ...

func LinearBurn

func LinearBurn(dst, src color.Color) color.Color

LinearBurn ...

func LinearDodge

func LinearDodge(dst, src color.Color) color.Color

LinearDodge ...

func LinearLight

func LinearLight(dst, src color.Color) color.Color

LinearLight ...

func Luminosity

func Luminosity(dst, src color.Color) color.Color

Luminosity ...

func Multiply

func Multiply(dst, src color.Color) color.Color

Multiply ...

func Overlay

func Overlay(dst, src color.Color) color.Color

Overlay ...

func Phoenix

func Phoenix(dst, src color.Color) color.Color

Phoenix ...

func PinLight

func PinLight(dst, src color.Color) color.Color

PinLight ...

func Reflex

func Reflex(dst, src color.Color) color.Color

Reflex (a.k.a. Glow)

func Saturation

func Saturation(dst, src color.Color) color.Color

Saturation ...

func Screen

func Screen(dst, src color.Color) color.Color

Screen ...

func SoftLight

func SoftLight(dst, src color.Color) color.Color

SoftLight ...

func Substract

func Substract(dst, src color.Color) color.Color

Substract ...

func VividLight

func VividLight(dst, src color.Color) color.Color

VividLight ...

Types

type BlendFunc

type BlendFunc func(dst, src color.Color) color.Color

BlendFunc or blend mode receives a destination color and a source color, then returns a transformation of them. Blend() function receives a BlendFunc and applies it to every pixel in the overlaping areas of two given images.

type Renderer

type Renderer interface {
	draw.Image
	Render(x, y int)
}

Work in progress.

type ThreadedRenderer

type ThreadedRenderer interface {
	draw.Image
	ThreadedRender(x, y chan int, done chan bool)
}

Work in progress.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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