identiconpp  0.4.0
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< const 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...
 

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< const 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 }
29 : _rows(rows)
30 , _columns(columns)
31 , _type(type)
32 , _background(background)
33 , _foreground(foreground)
34 , _padding(padding)
35 {
36  check_color(_background);
37 
38  for (const string &color : _foreground)
39  {
40  check_color(color);
41  }
42 
43  if (_foreground.size() == 0)
44  {
45  throw std::invalid_argument
46  (
47  "You must specify at least 1 foreground color."
48  );
49  }
50 }

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

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