Fossil SCM

Improvements to the dark-mode color inversion algorthm in Pikchr.

drh 2020-11-09 20:45 trunk
Commit 919fa97ed405a337c68ae72f50fb056a672f6f24b140cb14b6840ab309eaf02a
1 file changed +22 -9
+22 -9
--- src/pikchr.c
+++ src/pikchr.c
@@ -411,11 +411,11 @@
411411
static void pik_append_x(Pik*,const char*,PNum,const char*);
412412
static void pik_append_y(Pik*,const char*,PNum,const char*);
413413
static void pik_append_xy(Pik*,const char*,PNum,PNum);
414414
static void pik_append_dis(Pik*,const char*,PNum,const char*);
415415
static void pik_append_arc(Pik*,PNum,PNum,PNum,PNum);
416
-static void pik_append_clr(Pik*,const char*,PNum,const char*);
416
+static void pik_append_clr(Pik*,const char*,PNum,const char*,int);
417417
static void pik_append_style(Pik*,PObj*,int);
418418
static void pik_append_txt(Pik*,PObj*, PBox*);
419419
static void pik_draw_arrowhead(Pik*,PPoint*pFrom,PPoint*pTo,PObj*);
420420
static void pik_chop(PPoint*pFrom,PPoint*pTo,PNum);
421421
static void pik_error(Pik*,PToken*,const char*);
@@ -4458,11 +4458,11 @@
44584458
bx = f->x + e1*dx;
44594459
by = f->y + e1*dy;
44604460
pik_append_xy(p,"<polygon points=\"", t->x, t->y);
44614461
pik_append_xy(p," ",bx-ddx, by-ddy);
44624462
pik_append_xy(p," ",bx+ddx, by+ddy);
4463
- pik_append_clr(p,"\" style=\"fill:",pObj->color,"\"/>\n");
4463
+ pik_append_clr(p,"\" style=\"fill:",pObj->color,"\"/>\n",0);
44644464
pik_chop(f,t,h/2);
44654465
}
44664466
44674467
/*
44684468
** Compute the relative offset to an edge location from the reference for a
@@ -4569,11 +4569,11 @@
45694569
}
45704570
45714571
/*
45724572
** Invert the RGB color so that it is appropriate for dark mode.
45734573
*/
4574
-static int pik_color_to_dark_mode(int x){
4574
+static int pik_color_to_dark_mode(int x, int isBg){
45754575
int r, g, b;
45764576
int mn, mx;
45774577
x = 0xffffff - x;
45784578
r = (x>>16) & 0xff;
45794579
g = (x>>8) & 0xff;
@@ -4585,10 +4585,23 @@
45854585
if( g<mn ) mn = g;
45864586
if( b<mn ) mn = b;
45874587
r = mn + (mx-r);
45884588
g = mn + (mx-g);
45894589
b = mn + (mx-b);
4590
+ if( isBg ){
4591
+ if( mx>127 ){
4592
+ r = (127*r)/mx;
4593
+ g = (127*g)/mx;
4594
+ b = (127*b)/mx;
4595
+ }
4596
+ }else{
4597
+ if( mn<128 ){
4598
+ r = 127 + ((r-mn)*128)/(mx-mn);
4599
+ g = 127 + ((g-mn)*128)/(mx-mn);
4600
+ b = 127 + ((b-mn)*128)/(mx-mn);
4601
+ }
4602
+ }
45904603
return r*0x10000 + g*0x100 + b;
45914604
}
45924605
45934606
/* Append a PNum value surrounded by text. Do coordinate transformations
45944607
** on the value.
@@ -4620,15 +4633,15 @@
46204633
char buf[200];
46214634
snprintf(buf, sizeof(buf)-1, "%s%g%s", z1, p->rScale*v, z2);
46224635
buf[sizeof(buf)-1] = 0;
46234636
pik_append(p, buf, -1);
46244637
}
4625
-static void pik_append_clr(Pik *p, const char *z1, PNum v, const char *z2){
4638
+static void pik_append_clr(Pik *p,const char *z1,PNum v,const char *z2,int bg){
46264639
char buf[200];
46274640
int x = (int)v;
46284641
int r, g, b;
4629
- if( p->mFlags & PIKCHR_DARK_MODE ) x = pik_color_to_dark_mode(x);
4642
+ if( p->mFlags & PIKCHR_DARK_MODE ) x = pik_color_to_dark_mode(x,bg);
46304643
r = (x>>16) & 0xff;
46314644
g = (x>>8) & 0xff;
46324645
b = x & 0xff;
46334646
snprintf(buf, sizeof(buf)-1, "%srgb(%d,%d,%d)%s", z1, r, g, b, z2);
46344647
buf[sizeof(buf)-1] = 0;
@@ -4654,21 +4667,21 @@
46544667
** the caller wants to add some more.
46554668
*/
46564669
static void pik_append_style(Pik *p, PObj *pObj, int bFill){
46574670
pik_append(p, " style=\"", -1);
46584671
if( pObj->fill>=0 && bFill ){
4659
- pik_append_clr(p, "fill:", pObj->fill, ";");
4672
+ pik_append_clr(p, "fill:", pObj->fill, ";",1);
46604673
}else{
46614674
pik_append(p,"fill:none;",-1);
46624675
}
46634676
if( pObj->sw>0.0 && pObj->color>=0.0 ){
46644677
PNum sw = pObj->sw;
46654678
pik_append_dis(p, "stroke-width:", sw, ";");
46664679
if( pObj->nPath>2 && pObj->rad<=pObj->sw ){
46674680
pik_append(p, "stroke-linejoin:round;", -1);
46684681
}
4669
- pik_append_clr(p, "stroke:",pObj->color,";");
4682
+ pik_append_clr(p, "stroke:",pObj->color,";",0);
46704683
if( pObj->dotted>0.0 ){
46714684
PNum v = pObj->dotted;
46724685
if( sw<2.1/p->rScale ) sw = 2.1/p->rScale;
46734686
pik_append_dis(p,"stroke-dasharray:",sw,"");
46744687
pik_append_dis(p,",",v,";");
@@ -4919,11 +4932,11 @@
49194932
}
49204933
if( t->eCode & TP_BOLD ){
49214934
pik_append(p, " font-weight=\"bold\"", -1);
49224935
}
49234936
if( pObj->color>=0.0 ){
4924
- pik_append_clr(p, " fill=\"", pObj->color, "\"");
4937
+ pik_append_clr(p, " fill=\"", pObj->color, "\"",0);
49254938
}
49264939
xtraFontScale *= p->fontScale;
49274940
if( xtraFontScale<=0.99 || xtraFontScale>=1.01 ){
49284941
pik_append_num(p, " font-size=\"", xtraFontScale*100.0);
49294942
pik_append(p, "%\"", 2);
@@ -7826,6 +7839,6 @@
78267839
78277840
78287841
#endif /* PIKCHR_TCL */
78297842
78307843
7831
-#line 7856 "pikchr.c"
7844
+#line 7869 "pikchr.c"
78327845
--- src/pikchr.c
+++ src/pikchr.c
@@ -411,11 +411,11 @@
411 static void pik_append_x(Pik*,const char*,PNum,const char*);
412 static void pik_append_y(Pik*,const char*,PNum,const char*);
413 static void pik_append_xy(Pik*,const char*,PNum,PNum);
414 static void pik_append_dis(Pik*,const char*,PNum,const char*);
415 static void pik_append_arc(Pik*,PNum,PNum,PNum,PNum);
416 static void pik_append_clr(Pik*,const char*,PNum,const char*);
417 static void pik_append_style(Pik*,PObj*,int);
418 static void pik_append_txt(Pik*,PObj*, PBox*);
419 static void pik_draw_arrowhead(Pik*,PPoint*pFrom,PPoint*pTo,PObj*);
420 static void pik_chop(PPoint*pFrom,PPoint*pTo,PNum);
421 static void pik_error(Pik*,PToken*,const char*);
@@ -4458,11 +4458,11 @@
4458 bx = f->x + e1*dx;
4459 by = f->y + e1*dy;
4460 pik_append_xy(p,"<polygon points=\"", t->x, t->y);
4461 pik_append_xy(p," ",bx-ddx, by-ddy);
4462 pik_append_xy(p," ",bx+ddx, by+ddy);
4463 pik_append_clr(p,"\" style=\"fill:",pObj->color,"\"/>\n");
4464 pik_chop(f,t,h/2);
4465 }
4466
4467 /*
4468 ** Compute the relative offset to an edge location from the reference for a
@@ -4569,11 +4569,11 @@
4569 }
4570
4571 /*
4572 ** Invert the RGB color so that it is appropriate for dark mode.
4573 */
4574 static int pik_color_to_dark_mode(int x){
4575 int r, g, b;
4576 int mn, mx;
4577 x = 0xffffff - x;
4578 r = (x>>16) & 0xff;
4579 g = (x>>8) & 0xff;
@@ -4585,10 +4585,23 @@
4585 if( g<mn ) mn = g;
4586 if( b<mn ) mn = b;
4587 r = mn + (mx-r);
4588 g = mn + (mx-g);
4589 b = mn + (mx-b);
 
 
 
 
 
 
 
 
 
 
 
 
 
4590 return r*0x10000 + g*0x100 + b;
4591 }
4592
4593 /* Append a PNum value surrounded by text. Do coordinate transformations
4594 ** on the value.
@@ -4620,15 +4633,15 @@
4620 char buf[200];
4621 snprintf(buf, sizeof(buf)-1, "%s%g%s", z1, p->rScale*v, z2);
4622 buf[sizeof(buf)-1] = 0;
4623 pik_append(p, buf, -1);
4624 }
4625 static void pik_append_clr(Pik *p, const char *z1, PNum v, const char *z2){
4626 char buf[200];
4627 int x = (int)v;
4628 int r, g, b;
4629 if( p->mFlags & PIKCHR_DARK_MODE ) x = pik_color_to_dark_mode(x);
4630 r = (x>>16) & 0xff;
4631 g = (x>>8) & 0xff;
4632 b = x & 0xff;
4633 snprintf(buf, sizeof(buf)-1, "%srgb(%d,%d,%d)%s", z1, r, g, b, z2);
4634 buf[sizeof(buf)-1] = 0;
@@ -4654,21 +4667,21 @@
4654 ** the caller wants to add some more.
4655 */
4656 static void pik_append_style(Pik *p, PObj *pObj, int bFill){
4657 pik_append(p, " style=\"", -1);
4658 if( pObj->fill>=0 && bFill ){
4659 pik_append_clr(p, "fill:", pObj->fill, ";");
4660 }else{
4661 pik_append(p,"fill:none;",-1);
4662 }
4663 if( pObj->sw>0.0 && pObj->color>=0.0 ){
4664 PNum sw = pObj->sw;
4665 pik_append_dis(p, "stroke-width:", sw, ";");
4666 if( pObj->nPath>2 && pObj->rad<=pObj->sw ){
4667 pik_append(p, "stroke-linejoin:round;", -1);
4668 }
4669 pik_append_clr(p, "stroke:",pObj->color,";");
4670 if( pObj->dotted>0.0 ){
4671 PNum v = pObj->dotted;
4672 if( sw<2.1/p->rScale ) sw = 2.1/p->rScale;
4673 pik_append_dis(p,"stroke-dasharray:",sw,"");
4674 pik_append_dis(p,",",v,";");
@@ -4919,11 +4932,11 @@
4919 }
4920 if( t->eCode & TP_BOLD ){
4921 pik_append(p, " font-weight=\"bold\"", -1);
4922 }
4923 if( pObj->color>=0.0 ){
4924 pik_append_clr(p, " fill=\"", pObj->color, "\"");
4925 }
4926 xtraFontScale *= p->fontScale;
4927 if( xtraFontScale<=0.99 || xtraFontScale>=1.01 ){
4928 pik_append_num(p, " font-size=\"", xtraFontScale*100.0);
4929 pik_append(p, "%\"", 2);
@@ -7826,6 +7839,6 @@
7826
7827
7828 #endif /* PIKCHR_TCL */
7829
7830
7831 #line 7856 "pikchr.c"
7832
--- src/pikchr.c
+++ src/pikchr.c
@@ -411,11 +411,11 @@
411 static void pik_append_x(Pik*,const char*,PNum,const char*);
412 static void pik_append_y(Pik*,const char*,PNum,const char*);
413 static void pik_append_xy(Pik*,const char*,PNum,PNum);
414 static void pik_append_dis(Pik*,const char*,PNum,const char*);
415 static void pik_append_arc(Pik*,PNum,PNum,PNum,PNum);
416 static void pik_append_clr(Pik*,const char*,PNum,const char*,int);
417 static void pik_append_style(Pik*,PObj*,int);
418 static void pik_append_txt(Pik*,PObj*, PBox*);
419 static void pik_draw_arrowhead(Pik*,PPoint*pFrom,PPoint*pTo,PObj*);
420 static void pik_chop(PPoint*pFrom,PPoint*pTo,PNum);
421 static void pik_error(Pik*,PToken*,const char*);
@@ -4458,11 +4458,11 @@
4458 bx = f->x + e1*dx;
4459 by = f->y + e1*dy;
4460 pik_append_xy(p,"<polygon points=\"", t->x, t->y);
4461 pik_append_xy(p," ",bx-ddx, by-ddy);
4462 pik_append_xy(p," ",bx+ddx, by+ddy);
4463 pik_append_clr(p,"\" style=\"fill:",pObj->color,"\"/>\n",0);
4464 pik_chop(f,t,h/2);
4465 }
4466
4467 /*
4468 ** Compute the relative offset to an edge location from the reference for a
@@ -4569,11 +4569,11 @@
4569 }
4570
4571 /*
4572 ** Invert the RGB color so that it is appropriate for dark mode.
4573 */
4574 static int pik_color_to_dark_mode(int x, int isBg){
4575 int r, g, b;
4576 int mn, mx;
4577 x = 0xffffff - x;
4578 r = (x>>16) & 0xff;
4579 g = (x>>8) & 0xff;
@@ -4585,10 +4585,23 @@
4585 if( g<mn ) mn = g;
4586 if( b<mn ) mn = b;
4587 r = mn + (mx-r);
4588 g = mn + (mx-g);
4589 b = mn + (mx-b);
4590 if( isBg ){
4591 if( mx>127 ){
4592 r = (127*r)/mx;
4593 g = (127*g)/mx;
4594 b = (127*b)/mx;
4595 }
4596 }else{
4597 if( mn<128 ){
4598 r = 127 + ((r-mn)*128)/(mx-mn);
4599 g = 127 + ((g-mn)*128)/(mx-mn);
4600 b = 127 + ((b-mn)*128)/(mx-mn);
4601 }
4602 }
4603 return r*0x10000 + g*0x100 + b;
4604 }
4605
4606 /* Append a PNum value surrounded by text. Do coordinate transformations
4607 ** on the value.
@@ -4620,15 +4633,15 @@
4633 char buf[200];
4634 snprintf(buf, sizeof(buf)-1, "%s%g%s", z1, p->rScale*v, z2);
4635 buf[sizeof(buf)-1] = 0;
4636 pik_append(p, buf, -1);
4637 }
4638 static void pik_append_clr(Pik *p,const char *z1,PNum v,const char *z2,int bg){
4639 char buf[200];
4640 int x = (int)v;
4641 int r, g, b;
4642 if( p->mFlags & PIKCHR_DARK_MODE ) x = pik_color_to_dark_mode(x,bg);
4643 r = (x>>16) & 0xff;
4644 g = (x>>8) & 0xff;
4645 b = x & 0xff;
4646 snprintf(buf, sizeof(buf)-1, "%srgb(%d,%d,%d)%s", z1, r, g, b, z2);
4647 buf[sizeof(buf)-1] = 0;
@@ -4654,21 +4667,21 @@
4667 ** the caller wants to add some more.
4668 */
4669 static void pik_append_style(Pik *p, PObj *pObj, int bFill){
4670 pik_append(p, " style=\"", -1);
4671 if( pObj->fill>=0 && bFill ){
4672 pik_append_clr(p, "fill:", pObj->fill, ";",1);
4673 }else{
4674 pik_append(p,"fill:none;",-1);
4675 }
4676 if( pObj->sw>0.0 && pObj->color>=0.0 ){
4677 PNum sw = pObj->sw;
4678 pik_append_dis(p, "stroke-width:", sw, ";");
4679 if( pObj->nPath>2 && pObj->rad<=pObj->sw ){
4680 pik_append(p, "stroke-linejoin:round;", -1);
4681 }
4682 pik_append_clr(p, "stroke:",pObj->color,";",0);
4683 if( pObj->dotted>0.0 ){
4684 PNum v = pObj->dotted;
4685 if( sw<2.1/p->rScale ) sw = 2.1/p->rScale;
4686 pik_append_dis(p,"stroke-dasharray:",sw,"");
4687 pik_append_dis(p,",",v,";");
@@ -4919,11 +4932,11 @@
4932 }
4933 if( t->eCode & TP_BOLD ){
4934 pik_append(p, " font-weight=\"bold\"", -1);
4935 }
4936 if( pObj->color>=0.0 ){
4937 pik_append_clr(p, " fill=\"", pObj->color, "\"",0);
4938 }
4939 xtraFontScale *= p->fontScale;
4940 if( xtraFontScale<=0.99 || xtraFontScale>=1.01 ){
4941 pik_append_num(p, " font-size=\"", xtraFontScale*100.0);
4942 pik_append(p, "%\"", 2);
@@ -7826,6 +7839,6 @@
7839
7840
7841 #endif /* PIKCHR_TCL */
7842
7843
7844 #line 7869 "pikchr.c"
7845

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button