From da1c36736f98e8e7eea85cf200859f58f2683984 Mon Sep 17 00:00:00 2001 From: illyum <90023277+itzilly@users.noreply.github.com> Date: Fri, 16 Aug 2024 19:30:45 -0600 Subject: [PATCH] (Feat): Add Color Swatches --- src/ColorSwatch.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++ src/ColorSwatch.h | 33 +++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/ColorSwatch.cpp create mode 100644 src/ColorSwatch.h diff --git a/src/ColorSwatch.cpp b/src/ColorSwatch.cpp new file mode 100644 index 0000000..ab6cc65 --- /dev/null +++ b/src/ColorSwatch.cpp @@ -0,0 +1,59 @@ +// +// Created by illyum on 8/16/2024. +// + +#include "ColorSwatch.h" +#include +#include +#include "cJSON.h" + + +int SaveSwatch(const char *filename, const SavedSwatch *swatch) { + FILE *file = fopen(filename, "wb"); + if (file == NULL) { + return 0; + } + + fwrite(&swatch->count, sizeof(size_t), 1, file); + fwrite(swatch->colors, sizeof(SwatchColor), swatch->count, file); + + fclose(file); + return 1; +} + +int LoadSwatch(const char *filename, SavedSwatch *swatch) { + FILE *file = fopen(filename, "rb"); + if (file == NULL) { + return 0; + } + + fread(&swatch->count, sizeof(size_t), 1, file); + + swatch->colors = (SwatchColor *)malloc(swatch->count * sizeof(SwatchColor)); + if (swatch->colors == NULL) { + fclose(file); + return 0; + } + + fread(swatch->colors, sizeof(SwatchColor), swatch->count, file); + fclose(file); + return 1; +} + +void FreeSwatch(SavedSwatch *swatch) { + free(swatch->colors); + swatch->colors = NULL; + swatch->count = 0; +} + +SwatchColor ImVec4ToSwatchColor(const ImVec4& color) { + SwatchColor swatchColor; + swatchColor.r = static_cast(color.x * 255.0f); + swatchColor.g = static_cast(color.y * 255.0f); + swatchColor.b = static_cast(color.z * 255.0f); + return swatchColor; +} + +ImVec4 SwatchColorToImVec4(const SwatchColor& swatchColor) { + return {swatchColor.r / 255.0f, swatchColor.g / 255.0f, swatchColor.b / 255.0f, 1.0f}; +} diff --git a/src/ColorSwatch.h b/src/ColorSwatch.h new file mode 100644 index 0000000..921f1ee --- /dev/null +++ b/src/ColorSwatch.h @@ -0,0 +1,33 @@ +// +// Created by illyum on 8/16/2024. +// + +#ifndef COLORSWATCH_H +#define COLORSWATCH_H + +#include +#include +#include + + +typedef struct { + unsigned char r; + unsigned char g; + unsigned char b; +} SwatchColor; + +typedef struct { + std::string swatchName; + SwatchColor *colors; + size_t count; +} SavedSwatch; + +int SaveSwatch(const char *filename, const SavedSwatch *swatch); +int LoadSwatch(const char *filename, SavedSwatch *swatch); +void FreeSwatch(SavedSwatch *swatch); + +SwatchColor ImVec4ToSwatchColor(const ImVec4& color); + +ImVec4 SwatchColorToImVec4(const SwatchColor& swatchColor); + +#endif //COLORSWATCH_H