Copyright | (c) Christopher Wells, 2016 |
---|---|
License | MIT |
Maintainer | cwellsny@nycap.rr.com |
Safe Haskell | None |
Language | Haskell2010 |
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
- newtype Seed = Seed {}
- createSeed :: String -> Seed
- data Avatar = Avatar {
- color :: Color
- grid :: AvatarGrid
- generateAvatar :: Seed -> Avatar
- scaleAvatar :: Int -> Avatar -> Avatar
- saveAvatar :: Avatar -> FilePath -> IO ()
- saveAvatarWith :: ImageConversion -> Avatar -> FilePath -> IO ()
- convertAvatarToImage :: Avatar -> Image PixelRGB8
- type ImageConversion = Image PixelRGB8 -> ByteString
- encodeToPng :: ImageConversion
- encodeToGif :: ImageConversion
- encodeToTiff :: ImageConversion
- data Color
- getColorValue :: Color -> PixelRGB8
- colorFromSeed :: Seed -> Color
- newtype AvatarGrid = AvatarGrid {
- unAvatarGrid :: [[Bool]]
- showGrid :: [[Bool]] -> String
- generateAvatarGrid :: Seed -> AvatarGrid
- scaleList :: Int -> [a] -> [a]
Types
Seed
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.
createSeed :: String -> Seed Source #
Creates a seed from a given String.
>>>
createSeed "Hello"
Seed {unSeed = "8b1a9953c4611296a827abf8c47804d7"}
Avatar
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.
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
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
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.
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.
AvatarGrid | |
|
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"}
██ ██ ██ ██ ██ █ █ █ █ ██ ██ ████████ █ ██ █ █ █