(Feat): Add Color Swatches
This commit is contained in:
parent
1753df7652
commit
da1c36736f
59
src/ColorSwatch.cpp
Normal file
59
src/ColorSwatch.cpp
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
//
|
||||||
|
// Created by illyum on 8/16/2024.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "ColorSwatch.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#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<unsigned char>(color.x * 255.0f);
|
||||||
|
swatchColor.g = static_cast<unsigned char>(color.y * 255.0f);
|
||||||
|
swatchColor.b = static_cast<unsigned char>(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};
|
||||||
|
}
|
33
src/ColorSwatch.h
Normal file
33
src/ColorSwatch.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
//
|
||||||
|
// Created by illyum on 8/16/2024.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef COLORSWATCH_H
|
||||||
|
#define COLORSWATCH_H
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <string>
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
|
|
||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user