Fossil SCM

Improvements to /test-bgcolor to show the requested color and both light-mode and dark-mode background colors all at once. Also prepopulate the entry boxes with a selection of colors.

drh 2025-03-26 09:54 trunk
Commit a390c6ba5a32d18a40a1d34dbf5fcbba60caf73812425d70d41fef4d2806df73
3 files changed +1 -1 +67 -16 +1 -1
+1 -1
--- src/branch.c
+++ src/branch.c
@@ -870,11 +870,11 @@
870870
const char *zLastCkin = db_column_text(&q, 5);
871871
const char *zBgClr = db_column_text(&q, 6);
872872
char *zAge = human_readable_age(rNow - rMtime);
873873
sqlite3_int64 iMtime = (sqlite3_int64)(rMtime*86400.0);
874874
if( zMergeTo && zMergeTo[0]==0 ) zMergeTo = 0;
875
- if( zBgClr ) zBgClr = reasonable_bg_color(zBgClr);
875
+ if( zBgClr ) zBgClr = reasonable_bg_color(zBgClr, 0);
876876
if( zBgClr==0 ){
877877
if( zBranch==0 || strcmp(zBranch,"trunk")==0 ){
878878
zBgClr = 0;
879879
}else{
880880
zBgClr = hash_color(zBranch);
881881
--- src/branch.c
+++ src/branch.c
@@ -870,11 +870,11 @@
870 const char *zLastCkin = db_column_text(&q, 5);
871 const char *zBgClr = db_column_text(&q, 6);
872 char *zAge = human_readable_age(rNow - rMtime);
873 sqlite3_int64 iMtime = (sqlite3_int64)(rMtime*86400.0);
874 if( zMergeTo && zMergeTo[0]==0 ) zMergeTo = 0;
875 if( zBgClr ) zBgClr = reasonable_bg_color(zBgClr);
876 if( zBgClr==0 ){
877 if( zBranch==0 || strcmp(zBranch,"trunk")==0 ){
878 zBgClr = 0;
879 }else{
880 zBgClr = hash_color(zBranch);
881
--- src/branch.c
+++ src/branch.c
@@ -870,11 +870,11 @@
870 const char *zLastCkin = db_column_text(&q, 5);
871 const char *zBgClr = db_column_text(&q, 6);
872 char *zAge = human_readable_age(rNow - rMtime);
873 sqlite3_int64 iMtime = (sqlite3_int64)(rMtime*86400.0);
874 if( zMergeTo && zMergeTo[0]==0 ) zMergeTo = 0;
875 if( zBgClr ) zBgClr = reasonable_bg_color(zBgClr, 0);
876 if( zBgClr==0 ){
877 if( zBranch==0 || strcmp(zBranch,"trunk")==0 ){
878 zBgClr = 0;
879 }else{
880 zBgClr = hash_color(zBranch);
881
+67 -16
--- src/color.c
+++ src/color.c
@@ -184,12 +184,18 @@
184184
if( zName[0]=='#' ){
185185
int i, v = 0;
186186
for(i=1; i<=6 && fossil_isxdigit(zName[i]); i++){
187187
v = v*16 + fossil_hexvalue(zName[i]);
188188
}
189
- if( i<7 ) return -1;
190
- return v;
189
+ if( i==4 ){
190
+ for(v=0, i=1; i<4; i++) v = v*256 + fossil_hexvalue(zName[i]);
191
+ return v;
192
+ }
193
+ if( i==7 ){
194
+ return v;
195
+ }
196
+ return -1;
191197
}else if( sqlite3_strlike("rgb%)", zName,0)==0 ){
192198
return -1;
193199
}else if( sqlite3_strlike("hsl%)",zName,0)==0 ){
194200
return -1;
195201
}else{
@@ -216,19 +222,31 @@
216222
** The return value is a #HHHHHH color name contained in
217223
** static space that is overwritten on the next call.
218224
**
219225
** If we cannot make sense of the background color recommendation
220226
** that is the input, then return NULL.
227
+**
228
+** The iFgClr parameter is normally 0. But for testing purposes, set
229
+** it to 1 for a black foregrounds and 2 for a white foreground.
221230
*/
222
-char *reasonable_bg_color(const char *zRequested){
231
+char *reasonable_bg_color(const char *zRequested, int iFgClr){
223232
int iRGB = color_name_to_rgb(zRequested);
224233
int cc[3];
225234
int lo, hi;
226235
int r, g, b;
227
- static int fg = 0; /* 1==black-foreground 2==white-foreground */
236
+ static int systemFg = 0; /* 1==black-foreground 2==white-foreground */
237
+ int fg;
228238
static char zColor[10];
229239
int K = 70; /* Tune for background color saturation */
240
+
241
+ if( iFgClr ){
242
+ fg = iFgClr;
243
+ }else if( systemFg==0 ){
244
+ fg = systemFg = skin_detail_boolean("white-foreground") ? 2 : 1;
245
+ }else{
246
+ fg = systemFg;
247
+ }
230248
231249
if( iRGB<0 ) return 0;
232250
if( fg==0 ) fg = skin_detail_boolean("white-foreground") ? 2 : 1;
233251
cc[0] = (iRGB>>16) & 0xff;
234252
cc[1] = (iRGB>>8) & 0xff;
@@ -429,43 +447,76 @@
429447
** using the reasonable_bg_color() algorithm.
430448
*/
431449
void test_bgcolor_page(void){
432450
const char *zReq; /* Requested color name */
433451
const char *zBG; /* Actual color provided */
452
+ const char *zBg1;
434453
char zNm[10];
435
- int i, cnt;
454
+ static const char *azDflt[] = {
455
+ "red", "orange", "yellow", "green", "blue", "indigo", "violet",
456
+ "tan", "brown", "gray"
457
+ };
458
+ int i, cnt, iClr, r, g, b;
459
+ char *zFg;
436460
login_check_credentials();
437461
style_set_current_feature("test");
438462
style_header("Background Color Test");
439463
for(i=cnt=0; i<10; i++){
440464
sqlite3_snprintf(sizeof(zNm),zNm,"b%d",i);
441
- zReq = P(zNm);
465
+ zReq = PD(zNm,azDflt[i]);
442466
if( zReq==0 || zReq[0]==0 ) continue;
443
- zBG = reasonable_bg_color(zReq);
467
+ if( cnt==0 ){
468
+ @ <table border="1" cellspacing="0" cellpadding="10">
469
+ @ <tr>
470
+ @ <th>Requested Background
471
+ @ <th>Light mode
472
+ @ <th>Dark mode
473
+ @ </tr>
474
+ }
475
+ cnt++;
476
+ zBG = reasonable_bg_color(zReq, 0);
444477
if( zBG==0 ){
445
- @ <p>"%h(zReq)" is not a recognized color name</p>
446
- }else if( zReq[0]!='#' ){
478
+ @ <tr><td colspan="3" align="center">\
479
+ @ "%h(zReq)" is not a recognized color name</td></tr>
480
+ continue;
481
+ }
482
+ iClr = color_name_to_rgb(zReq);
483
+ r = (iClr>>16) & 0xff;
484
+ g = (iClr>>8) & 0xff;
485
+ b = iClr & 0xff;
486
+ if( 3*r + 6*g + b > 5*255 ){
487
+ zFg = "black";
488
+ }else{
489
+ zFg = "white";
490
+ }
491
+ if( zReq[0]!='#' ){
447492
char zReqRGB[12];
448493
sqlite3_snprintf(sizeof(zReqRGB),zReqRGB,"#%06x",color_name_to_rgb(zReq));
449
- @ <p style='border:1px solid;background-color:%s(zBG);'>
450
- @ Requested: %h(zReq) (%h(zReqRGB)) &rarr; Actual: %h(zBG)</p>
451
- cnt++;
494
+ @ <tr><td style='color:%h(zFg);background-color:%h(zReq);'>\
495
+ @ Requested color "%h(zReq)" (%h(zReqRGB))</td>
452496
}else{
453
- @ <p style='border:1px solid;background-color:%s(zBG);'>
454
- @ Requested: %h(zReq) &rarr; Actual: %h(zBG)</p>
455
- cnt++;
497
+ @ <tr><td style='color:%h(zFg);background-color:%s(zReq);'>\
498
+ @ Requested color "%h(zReq)"</td>
456499
}
500
+ zBg1 = reasonable_bg_color(zReq,1);
501
+ @ <td style='color:black;background-color:%h(zBg1);'>\
502
+ @ Background color for dark text: %h(zBg1)</td>
503
+ zBg1 = reasonable_bg_color(zReq,2);
504
+ @ <td style='color:white;background-color:%h(zBg1);'>\
505
+ @ Background color for light text: %h(zBg1)</td></tr>
457506
}
458507
if( cnt ){
508
+ @ </table>
459509
@ <hr>
460510
}
461511
@ <form method="POST">
462512
@ <p>Enter CSS color names below and see them shifted into corresponding
463513
@ background colors above.</p>
464514
for(i=0; i<10; i++){
465515
sqlite3_snprintf(sizeof(zNm),zNm,"b%d",i);
466
- @ <input type="text" size="30" name='%s(zNm)' value='%h(PD(zNm,""))'><br>
516
+ @ <input type="text" size="30" name='%s(zNm)' \
517
+ @ value='%h(PD(zNm,azDflt[i]))'><br>
467518
}
468519
@ <input type="submit" value="Submit">
469520
@ </form>
470521
style_finish_page();
471522
}
472523
--- src/color.c
+++ src/color.c
@@ -184,12 +184,18 @@
184 if( zName[0]=='#' ){
185 int i, v = 0;
186 for(i=1; i<=6 && fossil_isxdigit(zName[i]); i++){
187 v = v*16 + fossil_hexvalue(zName[i]);
188 }
189 if( i<7 ) return -1;
190 return v;
 
 
 
 
 
 
191 }else if( sqlite3_strlike("rgb%)", zName,0)==0 ){
192 return -1;
193 }else if( sqlite3_strlike("hsl%)",zName,0)==0 ){
194 return -1;
195 }else{
@@ -216,19 +222,31 @@
216 ** The return value is a #HHHHHH color name contained in
217 ** static space that is overwritten on the next call.
218 **
219 ** If we cannot make sense of the background color recommendation
220 ** that is the input, then return NULL.
 
 
 
221 */
222 char *reasonable_bg_color(const char *zRequested){
223 int iRGB = color_name_to_rgb(zRequested);
224 int cc[3];
225 int lo, hi;
226 int r, g, b;
227 static int fg = 0; /* 1==black-foreground 2==white-foreground */
 
228 static char zColor[10];
229 int K = 70; /* Tune for background color saturation */
 
 
 
 
 
 
 
 
230
231 if( iRGB<0 ) return 0;
232 if( fg==0 ) fg = skin_detail_boolean("white-foreground") ? 2 : 1;
233 cc[0] = (iRGB>>16) & 0xff;
234 cc[1] = (iRGB>>8) & 0xff;
@@ -429,43 +447,76 @@
429 ** using the reasonable_bg_color() algorithm.
430 */
431 void test_bgcolor_page(void){
432 const char *zReq; /* Requested color name */
433 const char *zBG; /* Actual color provided */
 
434 char zNm[10];
435 int i, cnt;
 
 
 
 
 
436 login_check_credentials();
437 style_set_current_feature("test");
438 style_header("Background Color Test");
439 for(i=cnt=0; i<10; i++){
440 sqlite3_snprintf(sizeof(zNm),zNm,"b%d",i);
441 zReq = P(zNm);
442 if( zReq==0 || zReq[0]==0 ) continue;
443 zBG = reasonable_bg_color(zReq);
 
 
 
 
 
 
 
 
 
444 if( zBG==0 ){
445 @ <p>"%h(zReq)" is not a recognized color name</p>
446 }else if( zReq[0]!='#' ){
 
 
 
 
 
 
 
 
 
 
 
 
447 char zReqRGB[12];
448 sqlite3_snprintf(sizeof(zReqRGB),zReqRGB,"#%06x",color_name_to_rgb(zReq));
449 @ <p style='border:1px solid;background-color:%s(zBG);'>
450 @ Requested: %h(zReq) (%h(zReqRGB)) &rarr; Actual: %h(zBG)</p>
451 cnt++;
452 }else{
453 @ <p style='border:1px solid;background-color:%s(zBG);'>
454 @ Requested: %h(zReq) &rarr; Actual: %h(zBG)</p>
455 cnt++;
456 }
 
 
 
 
 
 
457 }
458 if( cnt ){
 
459 @ <hr>
460 }
461 @ <form method="POST">
462 @ <p>Enter CSS color names below and see them shifted into corresponding
463 @ background colors above.</p>
464 for(i=0; i<10; i++){
465 sqlite3_snprintf(sizeof(zNm),zNm,"b%d",i);
466 @ <input type="text" size="30" name='%s(zNm)' value='%h(PD(zNm,""))'><br>
 
467 }
468 @ <input type="submit" value="Submit">
469 @ </form>
470 style_finish_page();
471 }
472
--- src/color.c
+++ src/color.c
@@ -184,12 +184,18 @@
184 if( zName[0]=='#' ){
185 int i, v = 0;
186 for(i=1; i<=6 && fossil_isxdigit(zName[i]); i++){
187 v = v*16 + fossil_hexvalue(zName[i]);
188 }
189 if( i==4 ){
190 for(v=0, i=1; i<4; i++) v = v*256 + fossil_hexvalue(zName[i]);
191 return v;
192 }
193 if( i==7 ){
194 return v;
195 }
196 return -1;
197 }else if( sqlite3_strlike("rgb%)", zName,0)==0 ){
198 return -1;
199 }else if( sqlite3_strlike("hsl%)",zName,0)==0 ){
200 return -1;
201 }else{
@@ -216,19 +222,31 @@
222 ** The return value is a #HHHHHH color name contained in
223 ** static space that is overwritten on the next call.
224 **
225 ** If we cannot make sense of the background color recommendation
226 ** that is the input, then return NULL.
227 **
228 ** The iFgClr parameter is normally 0. But for testing purposes, set
229 ** it to 1 for a black foregrounds and 2 for a white foreground.
230 */
231 char *reasonable_bg_color(const char *zRequested, int iFgClr){
232 int iRGB = color_name_to_rgb(zRequested);
233 int cc[3];
234 int lo, hi;
235 int r, g, b;
236 static int systemFg = 0; /* 1==black-foreground 2==white-foreground */
237 int fg;
238 static char zColor[10];
239 int K = 70; /* Tune for background color saturation */
240
241 if( iFgClr ){
242 fg = iFgClr;
243 }else if( systemFg==0 ){
244 fg = systemFg = skin_detail_boolean("white-foreground") ? 2 : 1;
245 }else{
246 fg = systemFg;
247 }
248
249 if( iRGB<0 ) return 0;
250 if( fg==0 ) fg = skin_detail_boolean("white-foreground") ? 2 : 1;
251 cc[0] = (iRGB>>16) & 0xff;
252 cc[1] = (iRGB>>8) & 0xff;
@@ -429,43 +447,76 @@
447 ** using the reasonable_bg_color() algorithm.
448 */
449 void test_bgcolor_page(void){
450 const char *zReq; /* Requested color name */
451 const char *zBG; /* Actual color provided */
452 const char *zBg1;
453 char zNm[10];
454 static const char *azDflt[] = {
455 "red", "orange", "yellow", "green", "blue", "indigo", "violet",
456 "tan", "brown", "gray"
457 };
458 int i, cnt, iClr, r, g, b;
459 char *zFg;
460 login_check_credentials();
461 style_set_current_feature("test");
462 style_header("Background Color Test");
463 for(i=cnt=0; i<10; i++){
464 sqlite3_snprintf(sizeof(zNm),zNm,"b%d",i);
465 zReq = PD(zNm,azDflt[i]);
466 if( zReq==0 || zReq[0]==0 ) continue;
467 if( cnt==0 ){
468 @ <table border="1" cellspacing="0" cellpadding="10">
469 @ <tr>
470 @ <th>Requested Background
471 @ <th>Light mode
472 @ <th>Dark mode
473 @ </tr>
474 }
475 cnt++;
476 zBG = reasonable_bg_color(zReq, 0);
477 if( zBG==0 ){
478 @ <tr><td colspan="3" align="center">\
479 @ "%h(zReq)" is not a recognized color name</td></tr>
480 continue;
481 }
482 iClr = color_name_to_rgb(zReq);
483 r = (iClr>>16) & 0xff;
484 g = (iClr>>8) & 0xff;
485 b = iClr & 0xff;
486 if( 3*r + 6*g + b > 5*255 ){
487 zFg = "black";
488 }else{
489 zFg = "white";
490 }
491 if( zReq[0]!='#' ){
492 char zReqRGB[12];
493 sqlite3_snprintf(sizeof(zReqRGB),zReqRGB,"#%06x",color_name_to_rgb(zReq));
494 @ <tr><td style='color:%h(zFg);background-color:%h(zReq);'>\
495 @ Requested color "%h(zReq)" (%h(zReqRGB))</td>
 
496 }else{
497 @ <tr><td style='color:%h(zFg);background-color:%s(zReq);'>\
498 @ Requested color "%h(zReq)"</td>
 
499 }
500 zBg1 = reasonable_bg_color(zReq,1);
501 @ <td style='color:black;background-color:%h(zBg1);'>\
502 @ Background color for dark text: %h(zBg1)</td>
503 zBg1 = reasonable_bg_color(zReq,2);
504 @ <td style='color:white;background-color:%h(zBg1);'>\
505 @ Background color for light text: %h(zBg1)</td></tr>
506 }
507 if( cnt ){
508 @ </table>
509 @ <hr>
510 }
511 @ <form method="POST">
512 @ <p>Enter CSS color names below and see them shifted into corresponding
513 @ background colors above.</p>
514 for(i=0; i<10; i++){
515 sqlite3_snprintf(sizeof(zNm),zNm,"b%d",i);
516 @ <input type="text" size="30" name='%s(zNm)' \
517 @ value='%h(PD(zNm,azDflt[i]))'><br>
518 }
519 @ <input type="submit" value="Submit">
520 @ </form>
521 style_finish_page();
522 }
523
+1 -1
--- src/timeline.c
+++ src/timeline.c
@@ -424,11 +424,11 @@
424424
}
425425
db_reset(&qdelta);
426426
}
427427
}else{
428428
/* Make sure the user-specified background color is reasonable */
429
- zBgClr = reasonable_bg_color(zBgClr);
429
+ zBgClr = reasonable_bg_color(zBgClr, 0);
430430
}
431431
if( zType[0]=='c'
432432
&& (pGraph || zBgClr==0 || (tmFlags & (TIMELINE_BRCOLOR|TIMELINE_DELTA))!=0)
433433
){
434434
zBr = branch_of_rid(rid);
435435
--- src/timeline.c
+++ src/timeline.c
@@ -424,11 +424,11 @@
424 }
425 db_reset(&qdelta);
426 }
427 }else{
428 /* Make sure the user-specified background color is reasonable */
429 zBgClr = reasonable_bg_color(zBgClr);
430 }
431 if( zType[0]=='c'
432 && (pGraph || zBgClr==0 || (tmFlags & (TIMELINE_BRCOLOR|TIMELINE_DELTA))!=0)
433 ){
434 zBr = branch_of_rid(rid);
435
--- src/timeline.c
+++ src/timeline.c
@@ -424,11 +424,11 @@
424 }
425 db_reset(&qdelta);
426 }
427 }else{
428 /* Make sure the user-specified background color is reasonable */
429 zBgClr = reasonable_bg_color(zBgClr, 0);
430 }
431 if( zType[0]=='c'
432 && (pGraph || zBgClr==0 || (tmFlags & (TIMELINE_BRCOLOR|TIMELINE_DELTA))!=0)
433 ){
434 zBr = branch_of_rid(rid);
435

Keyboard Shortcuts

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