Skip to content

Commit 3d2c171

Browse files
committedMay 10, 2022
Add fake-opacity support
1 parent d73a942 commit 3d2c171

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
 

‎README.md

+7
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ $(
3232
).css("background-color", "#e34234")
3333
.css("color", "#ee6")
3434
.appendTo("body");
35+
36+
$(
37+
"<p>",
38+
(json){ text: "Hex Color (opacity-emulation)" }
39+
).css("background-color", "#e3423511")
40+
.css("color", "#ee61")
41+
.appendTo("body");
3542
```

‎jquery.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#define BIG_STRING 16384
44
#define SHORT_STRING 16
55

6+
#ifndef TERMINAL_COLOR
7+
#define TERMINAL_COLOR 0x00, 0x00, 0x00
8+
#endif
9+
10+
static const unsigned char base_color[] = { TERMINAL_COLOR };
11+
612
struct jQueryRet {
713
void (*appendTo)(char*);
814
struct jQueryRet (*css)(char*, char*);
@@ -41,7 +47,7 @@ unsigned char get2digit(char *s) {
4147

4248
/* 0: successful, 1: failed */
4349
int col2seq(char *seq, char *col) {
44-
unsigned char r, g, b;
50+
unsigned char r, g, b, a = 255;
4551

4652
if (!strcmp(col, "black"))
4753
strcpy(seq, "0");
@@ -61,12 +67,18 @@ int col2seq(char *seq, char *col) {
6167
strcpy(seq, "7");
6268
else if (col[0] == '#') {
6369
switch(strlen(++col)) {
70+
case 4:
71+
a = expandhex(col[3]);
72+
6473
case 3:
6574
r = expandhex(col[0]),
6675
g = expandhex(col[1]),
6776
b = expandhex(col[2]);
6877
break;
6978

79+
case 8:
80+
a = get2digit(col+6);
81+
7082
case 6:
7183
r = get2digit(col),
7284
g = get2digit(col+2),
@@ -77,6 +89,13 @@ int col2seq(char *seq, char *col) {
7789
return 1;
7890
}
7991

92+
if (a != 255) {
93+
float fg = (float)a / 255;
94+
r = base_color[0] * (1-fg) + r * fg;
95+
g = base_color[1] * (1-fg) + g * fg;
96+
b = base_color[2] * (1-fg) + b * fg;
97+
}
98+
8099
sprintf(seq, "8;2;%d;%d;%d", r, g, b);
81100
} else return 1;
82101

‎main.c

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define TERMINAL_COLOR 0x1d, 0x1f, 0x21
12
#include "jquery.h"
23

34
signed main() {
@@ -27,4 +28,25 @@ signed main() {
2728
).css("background-color", "#e34234")
2829
.css("color", "#ee6")
2930
.appendTo("body");
31+
32+
$(
33+
"<p>",
34+
(json){ text: "Hex Color (opacity-emulation)" }
35+
).css("background-color", "#e3423488")
36+
.css("color", "#ee68")
37+
.appendTo("body");
38+
39+
$(
40+
"<p>",
41+
(json){ text: "Hex Color (opacity-emulation)" }
42+
).css("background-color", "#e3423444")
43+
.css("color", "#ee64")
44+
.appendTo("body");
45+
46+
$(
47+
"<p>",
48+
(json){ text: "Hex Color (opacity-emulation)" }
49+
).css("background-color", "#e3423522")
50+
.css("color", "#ee62")
51+
.appendTo("body");
3052
}

0 commit comments

Comments
 (0)