From f8dc17242ec2bc9cb11731f1fee6b9df0a76e00b Mon Sep 17 00:00:00 2001 From: murat Date: Sat, 31 Jan 2026 17:56:46 +0300 Subject: [PATCH] feature: add json export for color palette --- main.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/main.cpp b/main.cpp index cd343f7..8f1c8bf 100644 --- a/main.cpp +++ b/main.cpp @@ -140,6 +140,12 @@ int main(int argc, char *argv[]) { int paletteSize = std::min(5, (int)buckets.size()); + std::ofstream json("palette.json"); + + json << "{\n"; + json << " \"image\": \"" << argv[1] << "\",\n"; + json << " \"palette\": [\n"; + for (int i = 0; i < paletteSize; i++) { auto& b = buckets[i]; int r = b.r / b.count; @@ -149,8 +155,27 @@ int main(int argc, char *argv[]) { printf("\033[48;2;%d;%d;%dm \033[0m #%02X%02X%02X\n", r, g, bcol, r, g, bcol); + + json << " { " + << "\"r\": " << r << ", " + << "\"g\": " << g << ", " + << "\"b\": " << bcol << ", " + << "\"hex\": \""; + + char hexbuf[16]; + sprintf(hexbuf, "#%02X%02X%02X", r, g, bcol); + + json << hexbuf << "\" }"; + + if (i != paletteSize - 1) json << ","; + + json << "\n"; } + json << " ]\n"; + json << "}\n"; + json.close(); + stbi_image_free(data); return 0;