pixelated-avatar-generator-0.1.3: A library and application for generating pixelated avatars.

Copyright(c) Christopher Wells, 2016
LicenseMIT
Maintainercwellsny@nycap.rr.com
Safe HaskellNone
LanguageHaskell2010

Graphics.Avatars.Pixelated

Contents

Description

This module provides types and functions for creating and working with pixelated avatars.

Avatars can be generated by providing a Seed. Seeds can be created by passing a random String into createSeed. The given String is then turned into an MD5 checksum, which is used as the seed value.

Once a Seed has been created, an Avatar can be generated from it by passing it into generateAvatar. By default, Avatars start at a size of 8x8px. The size of an Avatar can be increased by passing it into scaleAvatar. Once you have scaled the Avatar to the size you want it to be it can then be saved to a file by passing it into saveAvatar.

By default, the saveAvatar function saves the avatar as a png image file, however you can also save avatars in other image formats by using saveAvatarWith. It allows you to specify the function to use to convert the avatar image into an image ByteString.

Example

The following is an example showing how to construct a function which will generate a 256x256px avatar from a given seed string, and save it at the given location.

import Graphics.Avatars.Pixelated

createAndSaveAvatar :: String -> FilePath -> IO ()
createAndSaveAvatar s path = saveAvatar avatar path
  where avatar = scaleAvatar 32 $ generateAvatar seed
        seed   = createSeed s

Synopsis

Types

Seed

newtype Seed Source #

A seed to use in generating an avatar. Can be created from a String by using the createSeed function.

Seeds are expected to be 32 character hexidecimal MD5 checksums.

Constructors

Seed 

Fields

Instances

Eq Seed Source # 

Methods

(==) :: Seed -> Seed -> Bool #

(/=) :: Seed -> Seed -> Bool #

Show Seed Source # 

Methods

showsPrec :: Int -> Seed -> ShowS #

show :: Seed -> String #

showList :: [Seed] -> ShowS #

createSeed :: String -> Seed Source #

Creates a seed from a given String.

>>> createSeed "Hello"
Seed {unSeed = "8b1a9953c4611296a827abf8c47804d7"}

Avatar

data Avatar Source #

A generated avatar, comprised of a color and a grid representing the visual pattern of the avatar image. Can be created from a seed using generateAvatar.

Avatars are generated at a size of 8x8px, and can be scaled up to larger image sizes by using the scaleAvatar function.

Constructors

Avatar 

Fields

Instances

Eq Avatar Source # 

Methods

(==) :: Avatar -> Avatar -> Bool #

(/=) :: Avatar -> Avatar -> Bool #

Show Avatar Source #

Generates a String containing the color and pattern of the avatar.

generateAvatar :: Seed -> Avatar Source #

Generates an avatar from the given seed.

>>> generateAvatar Seed {unSeed = "8b1a9953c4611296a827abf8c47804d7"}
Grey
██ ██ ██
██    ██
█      █
  █  █  
██    ██
████████
█  ██  █
  █  █  

scaleAvatar :: Int -> Avatar -> Avatar Source #

Scales the given Avatar by the given scaling factor.

For example, scaling an 8x8px avatar by a factor of 4 would transform it into a 32x32px avatar.

saveAvatar :: Avatar -> FilePath -> IO () Source #

Saves the given avatar as a png image file to the given file path. The filepath should be the path and name of the image file to be created, including the file extension.

For saving an avatar in a non-png format, use saveAvatarWith.

makeAvatar :: Seed -> FilePath -> IO ()
makeAvatar seed path = do
  let avatar = generateAvatar seed path
  saveAvatar avatar path

saveAvatarWith :: ImageConversion -> Avatar -> FilePath -> IO () Source #

Saves the given avatar to the given file location, using the given function to encode it into a specific image format.

Some examples of encoding functions are encodeToGif and encodeToTiff.

saveTiffAvatar :: Seed -> FilePath -> IO ()
saveTiffAvatar seed path = do
  let avatar = generateAvatar seed path
  saveAvatarWith encodeTiff avatar path

convertAvatarToImage :: Avatar -> Image PixelRGB8 Source #

Converts the given Avatar into an Image.

Image Conversion

type ImageConversion = Image PixelRGB8 -> ByteString Source #

A function which converts an image into a lazy ByteString.

encodeToPng :: ImageConversion Source #

Converts an image into a Png image ByteString.

encodeToGif :: ImageConversion Source #

Converts an image into a Gif image ByteString.

encodeToTiff :: ImageConversion Source #

Converts an image into a Tiff image ByteString.

Color

data Color Source #

A color of an avatar. The color of an avatar is the color that is applied to the pattern of the avatar when it is converted into an image.

Constructors

Black 
Blue 
Green 
Grey 
Orange 
Purple 
Red 
Yellow 

getColorValue :: Color -> PixelRGB8 Source #

Converts the given color into a RGB pixel representation.

>>> getColorValue Orange
PixelRGB8 255 140 65

colorFromSeed :: Seed -> Color Source #

Picks an avatar color using the given seed.

Each of the eight possible colors has roughly an equal chance of being chosen with a random seed.

>>> colorFromSeed $ Seed {unSeed = "8b1a9953c4611296a827abf8c47804d7"}
Grey

Avatar Grid

newtype AvatarGrid Source #

A grid of boolean values representing an Avatar. True values indicate colored pixels, and False values indicate blank pixels.

Constructors

AvatarGrid 

Fields

Instances

Eq AvatarGrid Source # 
Show AvatarGrid Source #

Converts the grid into a String representation.

showGrid :: [[Bool]] -> String Source #

Converts a grid of boolean values into a String representation.

>>> putStrLn $ showGrid [[True, False], [False, True]]
█ 
 █

generateAvatarGrid :: Seed -> AvatarGrid Source #

Generates an AvatarGrid using the given Seed.

It works by generating the left half of the grid using the contents of the Seed, and then mirroring the left half to create the full grid.

>>> generateAvatarGrid Seed {unSeed = "8b1a9953c4611296a827abf8c47804d7"}
██ ██ ██
██    ██
█      █
  █  █  
██    ██
████████
█  ██  █
  █  █  

Utility

scaleList :: Int -> [a] -> [a] Source #

Scales the given list by the given scaling factor.

>>> scaleList 2 [1, 2]
[1,1,2,2]
>>> scaleList 3 [0, 1]
[0,0,0,1,1,1]