MyNixOS website logo
Description

Flexible generation of identicons.

Identicon

License BSD3 Hackage Stackage Nightly Stackage LTS CI

The package implements a flexible framework for generation of identicons on top of Juicy Pixels.

Quick start

To use the package you usually need the following imports and language extensions:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}

import Codec.Picture -- JuicyPixels
import Data.ByteString (ByteString) -- we use strict byte strings
import Data.Proxy
import Data.Word (Word8)
import Graphics.Identicon -- core definitions
import Graphics.Identicon.Primitive -- some visual primitives

You first write a type that represents the total number of bytes your identicon consumes and the number of distinct visual components it has (they are called “layers” in the terminology of the package):

type MyIcon = Identicon 12 :+ Consumer 4 :+ Consumer 4 :+ Consumer 4

Here we have an identicon that needs 12 bytes to be generated. It has three consumers that take 4 bytes each and generate layers, i.e. visual objects (circles, squares, etc.).

The second step is to write implementation of every layer. We can use the primitives available out-of-the-box, they live in the Graphics.Identicon.Primitive module:

myImpl :: Implementation MyIcon
myImpl = Identicon :+ a :+ a :+ a
  where
    a :: Word8 -> Word8 -> Word8 -> Word8 -> Layer
    a r g b n =
      rsym $ onGrid 3 3 n $
        gradientXY (edge . mid) black (PixelRGB8 r g b)

Every byte is available to the layer-generating function as a distinct Word8 argument. The type system makes sure that:

  • you consume exactly as many bytes as you promised in the type of your identicon;

  • you have as many layers as you have described in the type of your identicon;

  • every function in your implementation has a correct signature (i.e. it takes as many Word8s as promised and produces a Layer in the end).

Mixing of layers and generation is handled by the library like this:

-- | Here is the function that generates your identicons. It's usually
-- convenient to wrap the 'renderIdenticon' function that comes with the
-- library.
genMyIdenticon ::
  -- | Identicon width
  Int ->
  -- | Identicon height
  Int ->
  -- | Input (some sort of hash or something)
  ByteString ->
  -- | Identicon, unless 'ByteString' is too short
  Maybe (Image PixelRGB8)
genMyIdenticon = renderIdenticon (Proxy :: Proxy MyIcon) myImpl

For more information head straight to the Haddocks. BTW, I have written a blog post about the package where I demonstrate some pictures generated with it.

Related packages

The following packages are designed to be used with identicon:

Contribution

Issues, bugs, and questions may be reported in the GitHub issue tracker for this project.

Pull requests are also welcome.

License

Copyright © 2016–present Mark Karpov

Distributed under BSD 3 clause license.

Metadata

Version

0.2.3

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows