core

package
v2.2.8 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2025 License: MIT Imports: 9 Imported by: 0

README

ASCII Image Generator Core

Core package for converting images to ASCII art in Go. Provides flexible configuration options for pixel-to-character mapping and output customization.

Features

  • Configurable pixel-to-character ratio
  • Customizable character sets
  • Set the color scheme for symbols and background, or keep the original colors
  • Context-aware processing

Usage

Basic Example
package main

import (
	"context"
	"image"
	"os"

	"github.com/fandasy/ASCIIimage/v2/core"
)

func main() {
	// Create default generation options
	opts := core.DefaultOptions()

	// Load your image (implement your own loader)
	img := loadImage("input.jpg")

	// Generate ASCII art image
	asciiImg, err := core.GenerateASCIIImage(context.Background(), img, opts)
	if err != nil {
		panic(err)
	}

	// Save the result (implement your own saver)
	saveImage("output.png", asciiImg)
}
Advanced Configuration
// Create custom generation options
opts := &core.Options{
    PixelRatio: core.PixelRatio{X: 2, Y: 3}, // 2x3 pixels → 1 ASCII char
    Chars: core.NewChars("01"),              // Custom character set
    Color: core.DefaultColor(),
}

// Applying a customization over a ready-made option
opts.WithFaceColor(color.RGBA{R: 122, G: 122, B: 122})

// Applying a setting to the default option
// opts := core.DefaultOptions().WithFaceColor(color.RGBA{R: 122, G: 122, B: 122})

asciiImg, err := core.GenerateASCIIImage(context.Background(), img, opts)

API Reference

Function
// GenerateASCIIImage converts an image to ASCII art
func GenerateASCIIImage(ctx context.Context, img image.Image, opts_ptr *Options) (image.Image, error)
Options
type Options struct {
    // PixelRatio defines how many original pixels map to one ASCII character
    PixelRatio PixelRatio // {X, Y}
    
    // Chars defines the character set to use (dark to light)
    Chars *Chars

    // Color specifies the foreground and background color scheme
    Color Color
}

// PixelRatio defines the pixel-to-character ratio
type PixelRatio struct {
    X, Y int
}

// Color represents color configuration for ASCII art rendering
//   - It ensures proper contrast between text (ascii char) and background
//   - When OriginalFace is true, it preserves the original pixel colors in output
type Color struct {
    // Face is the foreground/text color
    Face color.Color
    
    // Background is the canvas/background color
    Background color.Color

    // TransparentBackground removes the background
    TransparentBackground bool

    // OriginalFace preserves the source image colors
    OriginalFace bool
}
Character Sets
// Chars represents a character set mapping
type Chars [256]byte

// NewChars creates a new character set from a string (dark to light)
func NewChars(chars string) *Chars

// DefaultChars returns the default character set (@%#*+=:~-. )
func DefaultChars() *Chars

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Face = func() *basicfont.Face {
	copyFace := *basicfont.Face7x13
	face := &copyFace
	face.Width = 10
	face.Left = 2
	face.Advance = 10

	return face
}()

Face provides a modified font for ASCII art rendering with:

  • Character width: 10px
  • Left padding: 2px
  • Advance width: 10px

Functions

func GenerateASCIIImage added in v2.0.4

func GenerateASCIIImage(ctx context.Context, img image.Image, opts_ptr *Options) (image.Image, error)

GenerateASCIIImage converts an image to ASCII art. The conversion can be canceled using the provided context.

Parameters:

  • ctx: Context for cancellation
  • img: Source image to convert
  • opts: Conversion options (character set, pixel ratio, color)

Returns:

  • image.Image: Image containing the ASCII art
  • error: Context cancellation error if operation was interrupted

Types

type Chars

type Chars [256]byte

Chars represents a mapping of 256 brightness levels to ASCII characters. The characters are ordered from darkest (low brightness) to lightest (high brightness).

func DefaultChars

func DefaultChars() *Chars

DefaultChars returns the default character set: "@%#*+=:~-. "

func NewChars

func NewChars(chars string) *Chars

NewChars creates a new character set for brightness-to-ASCII conversion. Accepts only ASCII characters ordered from darkest to lightest.

Returns default character set ("@%#*+=:~-. ") if:

  • Input string is empty
  • Input contains non-ASCII characters

Example:

chars := NewChars(" .:-=+*#%@") // Light to dark gradient

type Color added in v2.1.0

type Color struct {
	// Face is the foreground/text color
	//  Ignored when OriginalFace is true.
	Face color.Color

	// Background is the canvas/background color
	Background color.Color

	// TransparentBackground removes the background
	TransparentBackground bool

	// OriginalFace preserves the source image colors
	OriginalFace bool
	// contains filtered or unexported fields
}

Color represents color configuration for ASCII art rendering

  • It ensures proper contrast between text (ascii char) and background
  • When OriginalFace is true, it preserves the original pixel colors in output

func DefaultColor added in v2.1.0

func DefaultColor() Color

DefaultColor returns the standard color scheme:

  • Black text on white background
  • TransparentBackground disabled (false)
  • OriginalFace colors disabled (false)
  • Uses grayscale colors for optimization

type Options

type Options struct {
	// PixelRatio defines how many original pixels map to one ASCII character
	// Format: X (width), Y (height) original pixels → 1 ASCII character
	PixelRatio PixelRatio

	// Chars defines the character set to use for brightness mapping
	Chars *Chars

	// Color specifies the foreground and background color scheme
	// If invalid or unset, defaults to black-on-white
	// Use DefaultColor() for standard scheme
	Color Color
}

Options configure the ASCII art generation process

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns the default conversion options:

  • PixelRatio: 1x1 (one source pixel per ASCII character)
  • Chars: Default character set ("@%#*+=:~-. ")
  • Color: Black text on white background

func (*Options) WithBackgroundColor added in v2.1.0

func (o *Options) WithBackgroundColor(c color.Color) *Options

func (*Options) WithChars added in v2.1.0

func (o *Options) WithChars(c *Chars) *Options

func (*Options) WithColor added in v2.1.0

func (o *Options) WithColor(color Color) *Options

func (*Options) WithFaceColor added in v2.1.0

func (o *Options) WithFaceColor(c color.Color) *Options

func (*Options) WithOriginalColor added in v2.2.6

func (o *Options) WithOriginalColor(b bool) *Options

func (*Options) WithPixelRatio added in v2.1.0

func (o *Options) WithPixelRatio(x, y int) *Options

func (*Options) WithPixelRatioX added in v2.2.5

func (o *Options) WithPixelRatioX(x int) *Options

func (*Options) WithPixelRatioY added in v2.2.5

func (o *Options) WithPixelRatioY(y int) *Options

func (*Options) WithTransparentBackground added in v2.2.7

func (o *Options) WithTransparentBackground(b bool) *Options

type PixelRatio

type PixelRatio struct {
	X, Y int // Horizontal and vertical sampling ratios
}

PixelRatio defines the sampling ratio between source image and ASCII output

func DefaultPixelRatio

func DefaultPixelRatio() PixelRatio

DefaultPixelRatio returns the default 1:1 pixel ratio

Jump to

Keyboard shortcuts

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