-
Notifications
You must be signed in to change notification settings - Fork 4
/
hellwal.c
2159 lines (1832 loc) · 62.3 KB
/
hellwal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* hellwal - v1.0.2 - MIT LICENSE
*
* [ ] TODO: gtk css?
* [ ] TODO: support for other OS's like Mac or Win
* ------------------------------------------------------------------------
* [x] TODO: tweaking options for generated colors (func + dark-light mode
* [x] TODO: bright & dark offset value as cmd line argument
* [x] TODO: support for already built themes (like gruvbox etc.)
* [x] TODO: do more pleasant color schemes
* [x] TODO: better light theme
* [x] TODO: print proper program usage
* [x] TODO: -r for random
* [x] TODO: -s for scripts
* [x] TODO: gen. colors
* [x] TODO: templating
* [x] TODO: parsing
*
* changelog v1.0.2:
* - changed ~~arc4random()~~ to rand(), because it's not available on all platforms
* - changed rand() to ~~arc4random()~~
* - created palettes caching
* - added '--no-cache' cmd line argument
* - fixed applying addtional arguments for themes
*
* changelog v1.0.1:
* - fixed loading incorrect number of channels from image, this casued unmatched palette
* - added proper support for light mode
* - added --debug cmd line argument for more verbose output
* - added --dark-offset and --bright-offset cmd line arguments
* - added --inverted cmd line argument, it inverts color palette colors
* - added --color -c argument. Now we have 3 modes: dark, light and color
* - added --gray-scale argument to manipulate 'grayness' of color palette
* - added --static-background arg to set static background color
* - added --static-foreground arg to set static foreground color
*
* changelog v1.0.0:
* - first decent verision to release.
* - combined couple of ways into one for generating colorpalette
* - added bins to obtains more precise colors
* - fixed: half of palette is no more white randomly
* - get rid of unecessary functions
*
* ----------------------------------------------------------------------------------------------------
*
* changelog v0.0.7:
* - improved colors, gen_palette() function is re-designed (again)
* - changed wallpaper gen_palette algorithm to median cut
* - fixed some templates
*
* changelog v0.0.6:
* - improved colors, gen_palette() function is re-designed
* - added in ./assests example script how you can use hellwal
* - removed colors.sh
*
* changelog v0.0.5:
* - fixed .hex, .rgb while using keyword like (background, foreground etc.)
* - themes adjustments
* - added rgb template
* - changed directories errors to warn, to allow to use same variable as path
*
* changelog v0.0.4:
* - combined logging palletes into one
* - support for --random (pics random image or template from specified directory)
* - support for executing scripts after running hellwal
* - added terminal template, adjusted colors.sh
* - better --help
*
* changelog v0.0.3:
* - removed TEMPLATE_ARG, OUTPUT_NAME_ARG - I've come to conclusion that it's useless.
* - changed how 'variables' are parsed. Now index of colors ex. "color|0|.hex" is depracated. Use "color0.hex" instead.
* - added support for static themes, for example you can add gruvbox, catppuccin etc.
* - added THEME_ARG, THEME_FOLDER_ARG
* - added keyword "wallpaper" for wallpaper path.
* - added example themes in ./themes folder
*
* changelog v0.0.2:
* - actually working and better generated colorschemes
* - silent output: -q or --quiet
* - variable |w|, that recognized in templates returns IMAGE_ARG path
* - support for both light/dark mode
* - added more example templates
*/
#include <math.h>
#include <time.h>
#include <glob.h>
#include <fcntl.h>
#include <stdio.h>
#include <libgen.h>
#include <dirent.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define HELL_PARSER_IMPLEMENTATION
#include "hell_parser.h"
/***
* MACROS
***/
/* just palette size */
#define PALETTE_SIZE 16
#define BINS 8
/* set default value for global char* variables */
#define SET_DEF(x, s) \
if (x == NULL) \
x = home_full_path(s); \
else \
x = home_full_path(x);
/***
* STRUCTURES
***/
/* IMG
*
* image structure that contains all image data,
* we need to create color palette
*/
typedef struct
{
/* With 3 channels it goes from 0 -> size:
* pixels[0] = Red;
* pixels[1] = Green;
* pixels[2] = Blue;
* pixels[3] = Red;
* .. And so on
*
* Also, we are using uint8_t instead of RGB structure
* here, only because I dont know how to do it.
*/
uint8_t *pixels;
size_t size; /* Size is width * height * channels(3) */
unsigned width;
unsigned height;
} IMG;
/* RGB,
* what can I say huh */
typedef struct
{
uint8_t R; // Red [0, 255]
uint8_t G; // Green [0, 255]
uint8_t B; // Blue [0, 255]
} RGB;
typedef struct {
float H; // Hue [0, 360]
float S; // Saturation [0, 1]
float L; // Luminance [0, 1]
} HSL;
/* PALETTE
*
* stores all RGB colors */
typedef struct
{
RGB colors[PALETTE_SIZE];
} PALETTE;
/* TEMPLATE
*
* helps read and write and store processed buffer*/
typedef struct
{
char *name;
char *path;
char *content;
} TEMPLATE;
/***
* GLOBAL VARIABLES
***/
/* these are being set in set_args() */
char HELLWAL_DELIM = '%';
char HELLWAL_DELIM_COUNT = 2;
/* image path, which will be used to create PALETTE */
char *IMAGE_ARG = NULL;
/* quiet arg, if NULL you will get verbose output,
* otherwise its going to print everthing normally
*/
char *QUIET_ARG = NULL;
/* darkmode, lightmode and colormode - darkmode is default */
char *DARK_ARG = NULL;
char *LIGHT_ARG = NULL;
char *COLOR_ARG = NULL;
/* folder that contains templates */
char *TEMPLATE_FOLDER_ARG = NULL;
/*
* output folder for generated templates,
* default one is in ~/.cache/hellwal/
*/
char *OUTPUT_ARG = NULL;
/* name of theme in THEME_FOLDER_ARG,
* or (in case was not found) path to file */
char *THEME_ARG = NULL;
/* folder that contains static themes */
char *THEME_FOLDER_ARG = NULL;
/* pick random THING (image or theme) of provided folder path:
* - provide path to IMG using *IMAGE_ARG
* - or *THEME_FOLDER_ARG
*/
char *RANDOM_ARG = NULL;
/* enables more verbose output, to see what's going on */
char *DEBUG_ARG = NULL;
/* run script after successfull hellwal job */
char *SCRIPT_ARG = NULL;
/* do not cache palette, do not read cached palettes */
char *NO_CACHE_ARG = NULL;
/* invert color palette colors */
char *INVERT_ARG = NULL;
/* Set Static Background colors */
RGB *STATIC_BG_ARG = NULL;
/* Set Static Foreground colors */
RGB *STATIC_FG_ARG = NULL;
/* defines 'grayness' of colorpalette */
float GRAY_SCALE_ARG = -1;
/* defines offsets to manipulate darkness and brightness */
float BRIGHTNESS_OFFSET_ARG = -1;
float DARKNESS_OFFSET_ARG = -1;
float OFFSET_GLOBAL = 0;
/* default color template to save cached themes */
char *CACHE_TEMPLATE = "\
\\%\\%wallpaper = %% wallpaper %% \\%\\%\n\
\
\\%\\%background = #%% background %% \\%\\%\n\
\\%\\%foreground = #%% foreground %% \\%\\%\n\
\\%\\%cursor = #%% cursor %% \\%\\%\n\
\\%\\%border = #%% border %% \\%\\%\n\
\
\\%\\%color0 = #%% color0.hex %% \\%\\%\n\
\\%\\%color1 = #%% color1.hex %% \\%\\%\n\
\\%\\%color2 = #%% color2.hex %% \\%\\%\n\
\\%\\%color3 = #%% color3.hex %% \\%\\%\n\
\\%\\%color4 = #%% color4.hex %% \\%\\%\n\
\\%\\%color5 = #%% color5.hex %% \\%\\%\n\
\\%\\%color6 = #%% color6.hex %% \\%\\%\n\
\\%\\%color7 = #%% color7.hex %% \\%\\%\n\
\\%\\%color8 = #%% color8.hex %% \\%\\%\n\
\\%\\%color9 = #%% color9.hex %% \\%\\%\n\
\\%\\%color10 = #%% color10.hex %% \\%\\%\n\
\\%\\%color11 = #%% color11.hex %% \\%\\%\n\
\\%\\%color12 = #%% color12.hex %% \\%\\%\n\
\\%\\%color13 = #%% color13.hex %% \\%\\%\n\
\\%\\%color14 = #%% color14.hex %% \\%\\%\n\
\\%\\%color15 = #%% color15.hex %% \\%\\%\n";
/***
* FUNCTIONS DECLARATIONS
***/
/* args */
int set_args(int argc, char *argv[]);
/* utils */
RGB clamp_rgb(RGB color);
uint8_t clamp_uint8(int value);
int is_between_01_float(const char *str);
char *rand_file(char *path);
char *home_full_path(const char* path);
void check_output_dir(char *path);
void remove_whitespaces(char *str);
void run_script(const char *script);
void hellwal_usage(const char *name);
/* logging */
void eu(const char *format, ...);
void err(const char *format, ...);
void warn(const char *format, ...);
void log_c(const char *format, ...);
/* IMG */
IMG *img_load(char *filename);
void img_free(IMG *img);
/* RGB */
void print_rgb(RGB col);
HSL rgb_to_hsl(RGB color);
void median_cut(RGB *colors, size_t *starts, size_t *ends, size_t *num_boxes, size_t target_boxes);
float calculate_luminance(RGB c);
float color_distance(RGB color1, RGB color2);
float calculate_color_distance(RGB a, RGB b);
int is_more_colorful(RGB a, RGB b);
int is_color_palette_var(char *name);
int hex_to_rgb(const char *hex, RGB *p);
int compare_luminance(const void *a, const void *b);
int get_channel(RGB *colors, size_t start, size_t end, int channel);
int is_color_too_similar(RGB *palette, int num_colors, RGB new_color);
RGB apply_offsets(RGB c);
RGB apply_grayscale(RGB c);
RGB to_grayscale(RGB color);
RGB darken_color(RGB color, float factor);
RGB lighten_color(RGB color, float factor);
RGB saturate_color(RGB color, float factor);
RGB adjust_luminance(RGB color, float factor);
RGB bin_to_color(int r_bin, int g_bin, int b_bin);
RGB average_color(IMG *img, size_t start, size_t end);
RGB blend_colors(const RGB color1, const RGB color2, float blend_factor);
RGB blend_with_brightness(RGB bright_color, RGB mix_color, float mix_ratio);
/* term, set for all active terminals ANSI escape codes */
void set_term_colors(PALETTE pal);
/* palettes */
PALETTE gen_palette(IMG *img);
PALETTE get_color_palette(PALETTE p);
char *palette_color(PALETTE pal, unsigned c, char *fmt);
int check_cached_palette(char *filepath, PALETTE *p);
void palette_write_cache(char *filepath, PALETTE *p);
void invert_palette(PALETTE *p);
void print_palette(PALETTE pal);
void reverse_palette(PALETTE *palette);
void palette_handle_dark_mode(PALETTE *p);
void palette_handle_light_mode(PALETTE *p);
void palette_handle_color_mode(PALETTE *p);
void apply_addtional_arguments(PALETTE *p);
void sort_palette_by_luminance(PALETTE *palette);
/* templates */
char *load_file(char *filename);
void process_templating(PALETTE pal);
size_t template_write(TEMPLATE *t, char *dir);
void process_template(TEMPLATE *t, PALETTE pal);
TEMPLATE **get_template_structure_dir(const char *dir_path, size_t *_size);
/* themes */
char *load_theme(char *themename);
PALETTE process_themeing(char *theme);
int process_theme(char *t, PALETTE *pal);
/***
* FUNCTIONS DECLARATIONS
***/
/* prints usage to stdout */
void hellwal_usage(const char *name)
{
printf("Usage:\n");
printf(" %s -i <image> [OPTIONS]\n\n", name);
printf("Options:\n");
printf(" -i, --image <image> Set image file\n");
printf(" -d, --dark Set dark mode (default)\n");
printf(" -l, --light Set light mode\n");
printf(" -c, --color Enable colorized mode (experimental)\n");
printf(" -v, --invert Invert colors in the palette\n");
printf(" -q, --quiet Suppress output\n");
printf(" -r, --random Pick random image or theme\n");
printf(" -s, --script <script> Execute script after running hellwal\n");
printf(" -f, --template-folder <dir> Set folder containing templates\n");
printf(" -o, --output <dir> Set output folder for generated templates\n");
printf(" -t, --theme <file> Set theme file or name\n");
printf(" -k, --theme-folder <value> Set folder containing themes\n");
printf(" -g, --gray-scale <value> Apply grayscale filter (0-1) (float)\n");
printf(" -n, --dark-offset <value> Adjust darkness offset (0-1) (float)\n");
printf(" -b, --bright-offset <value> Adjust brightness offset (0-1) (float)\n");
printf(" --, --static-background \"#hex\" Set static background\n");
printf(" --, --static-foreground \"#hex\" Set static foreground\n");
printf(" -h, --help Display this help and exit\n\n");
printf("Defaults:\n");
printf(" Template folder: ~/.config/hellwal/templates\n");
printf(" Theme folder: ~/.config/hellwal/themes\n");
printf(" Output folder: ~/.cache/hellwal/\n\n");
}
/* set given arguments */
int set_args(int argc, char *argv[])
{
int j = 0; /* 'int i' from for loop counter to track incomplete option error */
for (int i = 1; i < argc; i++)
{
j = i;
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
{
hellwal_usage(argv[0]);
exit(EXIT_SUCCESS);
}
else if ((strcmp(argv[i], "--image") == 0 || strcmp(argv[i], "-i") == 0))
{
if (i + 1 < argc)
IMAGE_ARG = argv[++i];
else {
argc = -1;
}
}
else if ((strcmp(argv[i], "--dark") == 0 || strcmp(argv[i], "-d") == 0))
{
/* anything other than NULL, makes dark mode */
DARK_ARG = "";
}
else if ((strcmp(argv[i], "--light") == 0 || strcmp(argv[i], "-l") == 0))
{
LIGHT_ARG = "";
}
else if ((strcmp(argv[i], "--color") == 0 || strcmp(argv[i], "-c") == 0))
{
COLOR_ARG = "";
}
else if ((strcmp(argv[i], "--invert") == 0 || strcmp(argv[i], "-v") == 0))
{
INVERT_ARG = "";
}
else if ((strcmp(argv[i], "--random") == 0 || strcmp(argv[i], "-r") == 0))
{
RANDOM_ARG = "";
}
else if ((strcmp(argv[i], "--quiet") == 0 || strcmp(argv[i], "-q") == 0))
{
QUIET_ARG = "";
}
else if (strcmp(argv[i], "--debug") == 0)
{
DEBUG_ARG = "";
}
else if (strcmp(argv[i], "--no-cache") == 0)
{
NO_CACHE_ARG = "";
}
else if ((strcmp(argv[i], "--template-folder") == 0 || strcmp(argv[i], "-f") == 0))
{
if (i + 1 < argc)
TEMPLATE_FOLDER_ARG = argv[++i];
else {
argc = -1;
}
}
else if ((strcmp(argv[i], "--output") == 0 || strcmp(argv[i], "-o") == 0))
{
if (i + 1 < argc)
OUTPUT_ARG = argv[++i];
else {
argc = -1;
}
}
else if ((strcmp(argv[i], "--theme") == 0 || strcmp(argv[i], "-t") == 0))
{
if (i + 1 < argc)
THEME_ARG = argv[++i];
else {
argc = -1;
}
}
else if ((strcmp(argv[i], "--theme-folder") == 0 || strcmp(argv[i], "-k") == 0))
{
if (i + 1 < argc)
THEME_FOLDER_ARG = argv[++i];
else {
argc = -1;
}
}
else if ((strcmp(argv[i], "--script") == 0 || strcmp(argv[i], "-s") == 0))
{
if (i + 1 < argc)
SCRIPT_ARG = argv[++i];
else
argc = -1;
}
else if ((strcmp(argv[i], "--dark-offset") == 0 || strcmp(argv[i], "-n") == 0))
{
if (i + 1 < argc)
{
if (is_between_01_float(argv[++i]))
DARKNESS_OFFSET_ARG = strtod(argv[i], NULL);
else
warn("Dark offset value have to be floating point number between 0-1!, skipping argument.");
}
else
argc = -1;
}
else if ((strcmp(argv[i], "--bright-offset") == 0 || strcmp(argv[i], "-b") == 0))
{
if (i + 1 < argc)
{
if (is_between_01_float(argv[++i]))
BRIGHTNESS_OFFSET_ARG = strtod(argv[i], NULL);
else
warn("Bright offset value have to be floating point number between 0-1!, skipping argument.");
}
else
argc = -1;
}
else if ((strcmp(argv[i], "--gray-scale") == 0 || strcmp(argv[i], "-g") == 0))
{
if (i + 1 < argc)
{
if (is_between_01_float(argv[++i]))
GRAY_SCALE_ARG = strtod(argv[i], NULL);
else
warn("Grayscale value have to be floating point number between 0-1!, skipping argument.");
}
else
argc = -1;
}
else if (strcmp(argv[i], "--static-background") == 0)
{
if (i + 1 < argc)
{
STATIC_BG_ARG = calloc(1, sizeof(RGB));
if (!hex_to_rgb(argv[++i], STATIC_BG_ARG))
{
free(STATIC_BG_ARG);
err("Failed to parse static background: %s", argv[i-1]);
}
}
else
{
argc = -1;
}
}
else if (strcmp(argv[i], "--static-foreground") == 0)
{
if (i + 1 < argc)
{
STATIC_FG_ARG = calloc(1, sizeof(RGB));
if (!hex_to_rgb(argv[++i], STATIC_FG_ARG))
{
free(STATIC_FG_ARG);
err("Failed to parse static foreground: %s", argv[i-1]);
}
}
else
{
argc = -1;
}
}
else {
eu("Unknown option: %s", argv[i]);
}
}
if (argc == -1)
err("Incomplete option: %s", argv[j]);
/* handle needed arguments and warns - idk how to do this the other way */
if (RANDOM_ARG != NULL && (THEME_FOLDER_ARG == NULL && IMAGE_ARG == NULL))
err("you have to specify --image to provide image folder or --theme-folder to use RANDOM");
if (IMAGE_ARG == NULL && THEME_ARG == NULL && ((THEME_FOLDER_ARG == NULL || TEMPLATE_FOLDER_ARG == NULL) && RANDOM_ARG == NULL))
err("You have to provide image file or theme!: --image, --theme, \n\t");
if ((THEME_ARG != NULL || THEME_FOLDER_ARG != NULL) && IMAGE_ARG != NULL)
{
if (THEME_FOLDER_ARG != NULL)
err("you cannot use both --image and --theme-folder");
else
err("you cannot use both --image and --theme");
}
if (RANDOM_ARG != NULL && THEME_ARG != NULL)
warn("specified theme is not used: \"%s\"", THEME_ARG);
if (THEME_ARG == NULL && THEME_FOLDER_ARG != NULL && RANDOM_ARG == NULL)
warn("specified theme folder is not used: \"%s\", you also have to provide --theme", THEME_FOLDER_ARG);
/* if RANDOM, get file */
if (RANDOM_ARG != NULL)
{
if (IMAGE_ARG != NULL)
IMAGE_ARG = rand_file(IMAGE_ARG);
else
THEME_ARG = rand_file(THEME_FOLDER_ARG);
}
/* set offset values - you can provide both, but they will interfier with each other */
if (DARKNESS_OFFSET_ARG != -1)
OFFSET_GLOBAL -= DARKNESS_OFFSET_ARG;
if (BRIGHTNESS_OFFSET_ARG != -1)
OFFSET_GLOBAL += BRIGHTNESS_OFFSET_ARG;
/* If not specified, set default ones */
SET_DEF(OUTPUT_ARG, "~/.cache/hellwal/");
SET_DEF(THEME_FOLDER_ARG , "~/.config/hellwal/themes");
SET_DEF(TEMPLATE_FOLDER_ARG, "~/.config/hellwal/templates");
return 0;
}
/*
* prints to stderr formatted output and exits with EXIT_FAILURE,
* also prints hellwal_usage()
*/
void eu(const char *format, ...)
{
fprintf(stderr, "\033[91m[ERROR]: ");
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fprintf(stderr, "\033[0m\n");
hellwal_usage("hellwal");
exit(EXIT_FAILURE);
}
/* prints to stderr formatted output and exits with EXIT_FAILURE */
void err(const char *format, ...)
{
va_list ap;
va_start(ap, format);
fprintf(stderr, "\033[91m[ERROR]: ");
vfprintf(stderr, format, ap);
fprintf(stderr, "\033[0m");
va_end(ap);
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
/* prints to stderr formatted output, but not exits */
void warn(const char *format, ...)
{
va_list ap;
va_start(ap, format);
fprintf(stderr, "\033[93m[WARN]: ");
vfprintf(stderr, format, ap);
fprintf(stderr, "\033[0m");
va_end(ap);
fprintf(stderr, "\n");
}
/* prints formatted output to stdout with colors */
void log_c(const char *format, ...)
{
if (QUIET_ARG != NULL)
return;
va_list ap;
va_start(ap, format);
const char *default_color = "\033[96m";
fprintf(stdout, "%s[INFO]: ", default_color);
vfprintf(stdout, format, ap);
fprintf(stdout, "\033[0m");
va_end(ap);
fprintf(stdout, "\n");
}
/* get full path from '~/' or other relative paths */
char* home_full_path(const char* path)
{
if (path[0] == '~')
{
const char* home = getenv("HOME");
if (home)
{
char* full_path = malloc(strlen(home) + strlen(path) + 1);
if (full_path)
{
sprintf(full_path, "%s%s", home, path + 1);
return full_path;
}
}
}
return strdup(path);
}
/*
* checks if output directory exists,
* if not creates it
*/
void check_output_dir(char *path)
{
struct stat st;
if (stat(path, &st) == -1)
mkdir(path, 0700);
}
/* run script from given path */
void run_script(const char *script)
{
if (script == NULL)
return;
log_c("Starting script: %s", script);
size_t exit_code = system(script);
if (exit_code == 0)
log_c("Script \"%s\" exited with code: %d", script, exit_code);
else
err("Script \"%s\" exited with code: %d", script, exit_code);
}
/* avoid exceeding max value */
uint8_t clamp_uint8(int value)
{
if (value < 0) return 0;
if (value > 255) return 255;
return (uint8_t)value;
}
/* avoid exceeding max value for each */
RGB clamp_rgb(RGB color)
{
return (RGB)
{
.R = clamp_uint8(color.R),
.G = clamp_uint8(color.G),
.B = clamp_uint8(color.B)
};
}
/* get random file from given path */
char *rand_file(char *path)
{
DIR *dir = opendir(path);
if (dir == NULL)
err("Cannot access directory %s: ", path);
struct dirent *entry;
char **files = NULL;
size_t count = 0;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG)
{
files = realloc(files, sizeof(char *) * (count + 1));
if (files == NULL)
{
perror("Memory allocation failed");
closedir(dir);
return NULL;
}
files[count++] = strdup(entry->d_name);
}
}
closedir(dir);
if (count == 0) {
free(files);
err("No files found in directory: %s\n", path);
}
srand((unsigned int)(time(NULL) ^ getpid()));
size_t r_idx = rand() % count;
char *choosen = calloc(1, strlen(path) + strlen(files[r_idx]) + 2);
sprintf(choosen, "%s/%s", path, strdup(files[r_idx]));
for (size_t i = 0; i < count; i++)
free(files[i]);
free(files);
return choosen;
}
/* removes whitespaces from buffer */
void remove_whitespaces(char *str)
{
if (!str) return;
char *read = str;
char *write = str;
while (*read) {
if (*read != ' ' && *read != '\t' && *read != '\n' && *read != '\r') {
*write++ = *read;
}
read++;
}
*write = '\0';
}
int is_between_01_float(const char *str)
{
char *end;
double number = strtod(str, &end);
if (*end != '\0')
return 0;
return (number >= 0.0 && number <= 1.0);
}
/* calculate how bright is color */
float calculate_luminance(RGB c)
{
return (0.2126 * c.R + 0.7152 * c.G + 0.0722 * c.B);
}
/* sort palette by luminance to spread out colors */
void sort_palette_by_luminance(PALETTE *palette)
{
qsort(palette->colors, PALETTE_SIZE/2, sizeof(RGB), compare_luminance);
}
/* compare two RGB colors */
int compare_luminance(const void *a, const void *b)
{
RGB *color_a = (RGB *)a;
RGB *color_b = (RGB *)b;
float lum_a = calculate_luminance(*color_a);
float lum_b = calculate_luminance(*color_b);
if (lum_a < lum_b)
return -1;
else if (lum_a > lum_b)
return 1;
else
return 0;
}
/* saturate a color */
RGB saturate_color(RGB color, float factor)
{
float max_val = fmaxf(color.R, fmaxf(color.G, color.B));
color.R = (uint8_t)(color.R + (max_val - color.R) * factor);
color.G = (uint8_t)(color.G + (max_val - color.G) * factor);
color.B = (uint8_t)(color.B + (max_val - color.B) * factor);
return color;
}
RGB to_grayscale(RGB color)
{
uint8_t gray_value = (uint8_t)(0.299f * color.R + 0.587f * color.G + 0.114f * color.B);
return (RGB){gray_value, gray_value, gray_value};
}
/* Convert RGB to hsl */
HSL rgb_to_hsl(RGB color)
{
float r = color.R / 255.0f;
float g = color.G / 255.0f;
float b = color.B / 255.0f;
float max_val = fmaxf(r, fmaxf(g, b));
float min_val = fminf(r, fminf(g, b));
float delta = max_val - min_val;
HSL hsl;
hsl.L = max_val;
if (max_val != 0)
{
hsl.S = delta / max_val; // Saturation
}
else
{
hsl.S = 0;
hsl.H = -1;
return hsl;
}
if (delta == 0)
{
hsl.H = 0;
}
else
{
if (r == max_val)
{
hsl.H = (g - b) / delta;
}
else if (g == max_val)
{
hsl.H = 2 + (b - r) / delta;
}
else
{
hsl.H = 4 + (r - g) / delta;
}
hsl.H *= 60;
if (hsl.H < 0)
{
hsl.H += 360;
}
}
return hsl;
}
/* function to reverse the palette, used when light mode is specified */
void reverse_palette(PALETTE *palette)
{
for (int i = 0; i < PALETTE_SIZE / 2; i++)
{
RGB temp = palette->colors[i];
palette->colors[i] = palette->colors[PALETTE_SIZE - 1 - i];
palette->colors[PALETTE_SIZE - 1 - i] = temp;
}
}
/* Get channel */
int get_channel(RGB *colors, size_t start, size_t end, int channel)
{
uint8_t min = 255, max = 0;
for (size_t i = start; i < end; i++)
{
uint8_t value = ((uint8_t *)&colors[i])[channel];
if (value < min) min = value;
if (value > max) max = value;
}
return max - min;
}
/* check Euclidean distance between two colors to ensure diversity */
float calculate_color_distance(RGB a, RGB b)
{
return sqrtf(powf(a.R - b.R, 2) + powf(a.G - b.G, 2) + powf(a.B - b.B, 2));
}
/* ensure that new color is not too similar to existing colors in the palette */
int is_color_too_similar(RGB *palette, int num_colors, RGB new_color)
{
for (int i = 0; i < num_colors; i++)
{
if (calculate_color_distance(palette[i], new_color) < 35)
return 1; // Color is too similar
}
return 0;
}
/* blend two colors together based on a blend factor */
RGB blend_colors(RGB c1, RGB c2, float weight) {
weight = (weight < 0.0f) ? 0.0f : (weight > 1.0f) ? 1.0f : weight; // Clamp weight
return clamp_rgb((RGB){
.R = (int)(c1.R * (1 - weight) + c2.R * weight),
.G = (int)(c1.G * (1 - weight) + c2.G * weight),
.B = (int)(c1.B * (1 - weight) + c2.B * weight)
});
}
/*
* blend two colors together based on a blend factor,
* bright mode - leave little color accent on white surface
*/
RGB blend_with_brightness(RGB bright_color, RGB mix_color, float mix_ratio)
{
if (mix_ratio < 0.0f) mix_ratio = 0.0f;
if (mix_ratio > 1.0f) mix_ratio = 1.0f;
RGB blended;
blended.R = bright_color.R + mix_ratio * (mix_color.R - bright_color.R);
blended.G = bright_color.G + mix_ratio * (mix_color.G - bright_color.G);
blended.B = bright_color.B + mix_ratio * (mix_color.B - bright_color.B);
uint8_t max_channel = blended.R > blended.G ?
(blended.R > blended.B ? blended.R : blended.B) :
(blended.G > blended.B ? blended.G : blended.B);
if (max_channel > 0 && max_channel < 255)
{
float adjustment = 255.0f / (float)max_channel;
blended.R = (uint8_t)(blended.R * adjustment);
blended.G = (uint8_t)(blended.G * adjustment);
blended.B = (uint8_t)(blended.B * adjustment);
}
return blended;
}
/* calculate the average color of a given pixel range in an image */