#include <generators/noiseGen.hh> using std::min; using std::max; namespace ty::gen { noiseGen::noiseGen(const int& w, const int& h) : baseGen(w, h), gen(rd()) { } noiseGen::~noiseGen() { } unsigned char noiseGen::minMax(const int& in) { return static_cast<unsigned char>(min(localMax, max(localMin, in))); } unsigned char noiseGen::getRandom(const char& c) { switch (c) { case 'r': { return static_cast<unsigned char>(dist[0](gen)); } case 'g': { return static_cast<unsigned char>(dist[1](gen)); } case 'b': { return static_cast<unsigned char>(dist[2](gen)); } } return static_cast<unsigned char>(0); } void noiseGen::runGen(vector<unsigned char>& image) { for (int i = 0; i < width * height; ++i) { image[i * 4] = getRandom('r'); image[i * 4 + 1] = getRandom('g'); image[i * 4 + 2] = getRandom('b'); image[i * 4 + 3] = 255; } } void noiseGen::loadJson(const json& j) { for (int i = 0; i < static_cast<int>(colors.size()); i++) { castJsonValues(j, colors[i], i * 2); } for (int i = 0; i < static_cast<int>(colorRange.size()); i += 2) { dist.push_back(uniform_int_distribution<unsigned char>(colorRange[i], colorRange[i + 1])); } } void noiseGen::castJsonValues(const json& j, const string& color, const int& indx) { if (auto key = "right" + color; j.contains(key)) { try { auto val = minMax(j[key].get<int>()); colorRange[indx + 1] = val; } catch (const json::type_error& e) { } } if (auto key = "left" + color; j.contains(key)) { try { auto val = minMax(j[key].get<int>()); colorRange[indx] = val > colorRange[indx + 1] ? colorRange[indx + 1] : val; } catch (const json::type_error& e) { } } } } //ty::gen