Documentation
¶
Index ¶
- Variables
- func GenerateASCIIImage(ctx context.Context, img image.Image, opts_ptr *Options) (image.Image, error)
- type Chars
- type Color
- type Options
- func (o *Options) WithBackgroundColor(c color.Color) *Options
- func (o *Options) WithChars(c *Chars) *Options
- func (o *Options) WithColor(color Color) *Options
- func (o *Options) WithFaceColor(c color.Color) *Options
- func (o *Options) WithOriginalColor(b bool) *Options
- func (o *Options) WithPixelRatio(x, y int) *Options
- func (o *Options) WithPixelRatioX(x int) *Options
- func (o *Options) WithPixelRatioY(y int) *Options
- func (o *Options) WithTransparentBackground(b bool) *Options
- type PixelRatio
Constants ¶
This section is empty.
Variables ¶
var Face = func() *basicfont.Face { copyFace := *basicfont.Face7x13 face := ©Face 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 ¶
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 (*Options) WithFaceColor ¶ added in v2.1.0
func (*Options) WithOriginalColor ¶ added in v2.2.6
func (*Options) WithPixelRatio ¶ added in v2.1.0
func (*Options) WithPixelRatioX ¶ added in v2.2.5
func (*Options) WithPixelRatioY ¶ added in v2.2.5
func (*Options) WithTransparentBackground ¶ added in v2.2.7
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