summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-06-17 11:06:47 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-06-17 11:07:24 +0300
commitaac0bc8a49d06fffcaca7a1fd3c35de035ac3e7c (patch)
tree51c822f45781ae4545db9c44f0cc926110fc253f
parent1455c26b793be67f5c47a7f7dbf06c9c263d8e2a (diff)
Color: Load palettes from a config file
IssueID #263
-rw-r--r--src/app.c1
-rw-r--r--src/ui/color.c80
-rw-r--r--src/ui/color.h2
3 files changed, 81 insertions, 2 deletions
diff --git a/src/app.c b/src/app.c
index 749d998e..3a1ee101 100644
--- a/src/app.c
+++ b/src/app.c
@@ -761,6 +761,7 @@ static void init_App_(iApp *d, int argc, char **argv) {
761 setupApplication_iOS(); 761 setupApplication_iOS();
762#endif 762#endif
763 init_Keys(); 763 init_Keys();
764 loadPalette_Color(dataDir_App_());
764 setThemePalette_Color(d->prefs.theme); /* default UI colors */ 765 setThemePalette_Color(d->prefs.theme); /* default UI colors */
765 loadPrefs_App_(d); 766 loadPrefs_App_(d);
766 load_Keys(dataDir_App_()); 767 load_Keys(dataDir_App_());
diff --git a/src/ui/color.c b/src/ui/color.c
index 6c51bc06..05ec1f6f 100644
--- a/src/ui/color.c
+++ b/src/ui/color.c
@@ -24,11 +24,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
24#include "root.h" 24#include "root.h"
25#include "app.h" 25#include "app.h"
26 26
27#include <the_Foundation/file.h>
28#include <the_Foundation/path.h>
27#include <the_Foundation/string.h> 29#include <the_Foundation/string.h>
28 30
29static const iColor transparent_; 31static const iColor transparent_;
30 32
31static const iColor darkPalette_[] = { 33static iColor darkPalette_[] = {
32 { 0, 0, 0, 255 }, 34 { 0, 0, 0, 255 },
33 { 40, 40, 40, 255 }, 35 { 40, 40, 40, 255 },
34 { 80, 80, 80, 255 }, 36 { 80, 80, 80, 255 },
@@ -47,7 +49,7 @@ static const iColor darkPalette_[] = {
47 { 0, 200, 0, 255 }, 49 { 0, 200, 0, 255 },
48}; 50};
49 51
50static const iColor lightPalette_[] = { 52static iColor lightPalette_[] = {
51 { 0, 0, 0, 255 }, 53 { 0, 0, 0, 255 },
52 { 75, 75, 75, 255 }, 54 { 75, 75, 75, 255 },
53 { 150, 150, 150, 255 }, 55 { 150, 150, 150, 255 },
@@ -804,3 +806,77 @@ iColor ansiForeground_Color(iRangecc escapeSequence, int fallback) {
804 } 806 }
805 return clr; 807 return clr;
806} 808}
809
810iBool loadPalette_Color(const char *path) {
811 iBool wasLoaded = iFalse;
812 iFile *f = newCStr_File(concatPath_CStr(path, "palette.txt"));
813 if (open_File(f, text_FileMode | readOnly_FileMode)) {
814 iColor *dstPal = darkPalette_;
815 iRangecc srcLine = iNullRange;
816 const iBlock *src = collect_Block(readAll_File(f));
817 while (nextSplit_Rangecc(range_Block(src), "\n", &srcLine)) {
818 iRangecc line = srcLine;
819 trim_Rangecc(&line);
820 if (isEmpty_Range(&line)) {
821 continue;
822 }
823 if (*line.start == '#') {
824 /* Control directive. */
825 line.start++;
826 trim_Rangecc(&line);
827 if (equalCase_Rangecc(line, "dark")) {
828 dstPal = darkPalette_;
829 }
830 else if (equalCase_Rangecc(line, "light")) {
831 dstPal = lightPalette_;
832 }
833 continue;
834 }
835 static const struct {
836 const char *label;
837 int paletteIndex;
838 } colors_[] = {
839 { "black:", 0 }, { "gray25:", 1 }, { "gray50:", 2 }, { "gray75:", 3 },
840 { "white:", 4 }, { "brown:", 5 }, { "orange:", 6 }, { "teal:", 7 },
841 { "cyan:", 8 }, { "yellow:", 9 }, { "red:", 10 }, { "magenta:", 11 },
842 { "blue:", 12 }, { "green:", 13 },
843 };
844 iForIndices(i, colors_) {
845 if (startsWithCase_Rangecc(line, colors_[i].label)) {
846 iColor *dst = &dstPal[colors_[i].paletteIndex];
847 line.start += strlen(colors_[i].label);
848 trim_Rangecc(&line);
849 if (!isEmpty_Range(&line)) {
850 if (*line.start == '#') {
851 /* Hexadecimal color. */
852 line.start++;
853 if (size_Range(&line) == 6) {
854 iBlock *vals = hexDecode_Rangecc(line);
855 iAssert(size_Block(vals) == 3);
856 const uint8_t *rgb = constData_Block(vals);
857 *dst = (iColor){ rgb[0], rgb[1], rgb[2], 255 };
858 delete_Block(vals);
859 }
860 else {
861 fprintf(stderr, "[Color] invalid custom color: %s\n",
862 cstr_Rangecc(line));
863 }
864 }
865 else {
866 unsigned int red = 0, green = 0, blue = 0;
867 sscanf(line.start, "%u %u %u", &red, &green, &blue);
868 if (red > 255 || green > 255 || blue > 255) {
869 fprintf(stderr, "[Color] RGB value(s) out of range: %s\n",
870 cstr_Rangecc(line));
871 }
872 *dst = (iColor){ red, green, blue, 255 };
873 }
874 }
875 }
876 }
877 }
878 wasLoaded = iTrue;
879 }
880 iRelease(f);
881 return wasLoaded;
882}
diff --git a/src/ui/color.h b/src/ui/color.h
index aafc1794..37ec49eb 100644
--- a/src/ui/color.h
+++ b/src/ui/color.h
@@ -244,7 +244,9 @@ iLocalDef void setHsl_Color(int color, iHSLColor hsl) {
244 set_Color(color, rgb_HSLColor(hsl)); 244 set_Color(color, rgb_HSLColor(hsl));
245} 245}
246 246
247iBool loadPalette_Color (const char *path);
247void setThemePalette_Color (enum iColorTheme theme); 248void setThemePalette_Color (enum iColorTheme theme);
248 249
249iColor ansiForeground_Color (iRangecc escapeSequence, int fallback); 250iColor ansiForeground_Color (iRangecc escapeSequence, int fallback);
250const char * escape_Color (int color); 251const char * escape_Color (int color);
252