identiconpp  0.6.1
Public Types | Public Member Functions | Friends | List of all members
Identiconpp Class Reference

Base class for identiconpp. More...

#include <identiconpp.hpp>

Public Types

enum  algorithm { algorithm::ltr_symmetric, algorithm::ltr_asymmetric, algorithm::sigil }
 List of identicon algorithms. More...
 

Public Member Functions

 Identiconpp (const uint8_t columns, const uint8_t rows, algorithm type=algorithm::ltr_symmetric, const string &background="ffffffff", const vector< string > &foreground={ "000000ff" }, const array< uint8_t, 2 > &padding={ 0, 0 })
 Initialises an instance of Identiconpp. More...
 
Magick::Image generate (const string &digest, const uint16_t width=100)
 Generates identicon from digest. More...
 
const string generate_base64 (const string &magick, const string &digest, const uint16_t width=100)
 Generates identicon from digest. More...
 

Friends

class Testiconpp
 

Detailed Description

Base class for identiconpp.

Use this class for all your identicons. Exceptions will be thrown on error.

Examples:
example.cpp.

Member Enumeration Documentation

◆ algorithm

List of identicon algorithms.

Enumerator
ltr_symmetric 

Generates symmetric (vertically mirrored) identicons.

ltr_asymmetric 

Generates asymmetric identicons.

sigil 

Generates the same results as sigil and pydenticon.

50  {
52  ltr_symmetric,
54  ltr_asymmetric,
60  sigil
61  };

Constructor & Destructor Documentation

◆ Identiconpp()

Identiconpp::Identiconpp ( const uint8_t  columns,
const uint8_t  rows,
algorithm  type = algorithm::ltr_symmetric,
const string &  background = "ffffffff",
const vector< string > &  foreground = { "000000ff" },
const array< uint8_t, 2 > &  padding = { 0, 0 } 
)
explicit

Initialises an instance of Identiconpp.

The instance can be used for creating identicons with differing image formats and sizes. The colors must consist of exactly 8 digits.

Parameters
columnsNumber of columns
rowsNumber of rows
typeThe algorithm to use
backgroundBackground color, hexadecimal, rrggbbaa
foregroundvector of foreground colors
paddingPadding in pixels { left & right, top & down }
Since
before 0.5.0
30 : _rows(rows)
31 , _columns(columns)
32 , _type(type)
33 , _background(background)
34 , _foreground(foreground)
35 , _padding(padding)
36 {
37  check_color(_background);
38 
39  for (const string &color : _foreground)
40  {
41  check_color(color);
42  }
43 
44  if (_foreground.size() == 0)
45  {
46  throw std::invalid_argument
47  (
48  "You must specify at least 1 foreground color."
49  );
50  }
51 }

Member Function Documentation

◆ generate()

Magick::Image Identiconpp::generate ( const string &  digest,
const uint16_t  width = 100 
)

Generates identicon from digest.

Parameters
digestThe pre-computed digest
widthThe width of the image in pixels
Returns
The image as Magick::Image
Since
before 0.5.0
Examples:
example.cpp.
54 {
55  ttdebug << "Using digest: " << digest << '\n';
56  check_entropy(digest, _type);
57  const std::int16_t imgwidth = width - _padding[0] * 2;
58  const std::int16_t imgheight =
59  std::round(static_cast<float>(imgwidth) / _columns * _rows);
60  ttdebug << "width: " << std::to_string(imgwidth)
61  << "+" << std::to_string(_padding[0] * 2)
62  << ", height: " << std::to_string(imgheight)
63  << "+" << std::to_string(_padding[1] * 2)
64  << "\n";
65  if (imgwidth <= 0 || imgheight <= 0)
66  {
67  throw std::invalid_argument("Width or height is zero or less.");
68  }
69  Magick::Image img;
70 
71  switch (_type)
72  {
74  {
75  img = generate_ltr_symmetric(digest);
76  break;
77  }
79  {
80  img = generate_ltr_asymmetric(digest);
81  break;
82  }
83  case algorithm::sigil:
84  {
85  img = generate_sigil(digest);
86  break;
87  }
88  }
89 
90  img.scale(Magick::Geometry(imgwidth, imgheight));
91  // The CompositeOperator prevents the background color to be affected by the
92  // frame color. See https://github.com/ImageMagick/ImageMagick/issues/647
93  img.compose(Magick::CompositeOperator::CopyCompositeOp);
94  img.matteColor(Magick::Color('#' + _background));
95  img.frame(Magick::Geometry(_padding[0], _padding[1]));
96  return img;
97 }
Generates symmetric (vertically mirrored) identicons.
Generates asymmetric identicons.

◆ generate_base64()

const string Identiconpp::generate_base64 ( const string &  magick,
const string &  digest,
const uint16_t  width = 100 
)

Generates identicon from digest.

Parameters
magickSee http://imagemagick.org/script/formats.php
digestThe pre-computed digest
widthThe width of the image in pixels
Returns
The image as base64-string
Since
0.5.0
102 {
103  Magick::Image img = generate(digest, width);
104  Magick::Blob blob;
105  img.magick(magick);
106  img.write(&blob);
107  return blob.base64();
108 }
Magick::Image generate(const string &digest, const uint16_t width=100)
Generates identicon from digest.
Definition: identiconpp.cpp:53

The documentation for this class was generated from the following files: