Fossil SCM

Improve the background color adjustment algorithm so that colors that are further out of range receive a proportionally greater adjustment. Also expand the range of allowed colors, slightly.

drh 2025-03-28 12:20 trunk
Commit 9d2e0d0802ed7e1252ef53c283a526618581b468a61a060d20f6516f712d55fd
1 file changed +33 -13
+33 -13
--- src/color.c
+++ src/color.c
@@ -259,19 +259,38 @@
259259
if( iRGB<0 ) return 0;
260260
r = (iRGB>>16) & 0xff;
261261
g = (iRGB>>8) & 0xff;
262262
b = iRGB & 0xff;
263263
if( fg==1 ){
264
- const int K = 70;
265
- r = (K*r)/255 + (255-K);
266
- g = (K*g)/255 + (255-K);
267
- b = (K*b)/255 + (255-K);
264
+ /* Dark text on a light background. Adjust so that
265
+ ** no color component is less than 255-K, resulting in
266
+ ** a pastel background color. Color adjustment is quadratic
267
+ ** so that colors that are further out of range have a greater
268
+ ** adjustment. */
269
+ const int K = 79;
270
+ int k, x, m;
271
+ m = r<g ? r : g;
272
+ if( m>b ) m = b;
273
+ k = (m*m)/255 + K;
274
+ x = 255 - k;
275
+ r = (k*r)/255 + x;
276
+ g = (k*g)/255 + x;
277
+ b = (k*b)/255 + x;
268278
}else{
269
- const int K = 90;
270
- r = (K*r)/255;
271
- g = (K*g)/255;
272
- b = (K*b)/255;
279
+ /* Light text on a dark background. Adjust so that
280
+ ** no color component is greater than K, resulting in
281
+ ** a low-intensity, low-saturation background color.
282
+ ** The color adjustment is quadratic so that colors that
283
+ ** are further out of range have a greater adjustment. */
284
+ const int K = 112;
285
+ int k, m;
286
+ m = r>g ? r : g;
287
+ if( m<b ) m = b;
288
+ k = 255 - (255-K)*(m*m)/65025;
289
+ r = (k*r)/255;
290
+ g = (k*g)/255;
291
+ b = (k*b)/255;
273292
}
274293
sqlite3_snprintf(8, zColor, "#%02x%02x%02x", r,g,b);
275294
return zColor;
276295
}
277296
@@ -451,19 +470,20 @@
451470
const char *zBG; /* Actual color provided */
452471
const char *zBg1;
453472
char zNm[10];
454473
static const char *azDflt[] = {
455474
"red", "orange", "yellow", "green", "blue", "indigo", "violet",
456
- "tan", "brown", "gray"
475
+ "tan", "brown", "gray",
457476
};
477
+ const int N = count(azDflt);
458478
int i, cnt, iClr, r, g, b;
459479
char *zFg;
460480
login_check_credentials();
461481
style_set_current_feature("test");
462482
style_header("Background Color Test");
463
- for(i=cnt=0; i<10; i++){
464
- sqlite3_snprintf(sizeof(zNm),zNm,"b%d",i);
483
+ for(i=cnt=0; i<N; i++){
484
+ sqlite3_snprintf(sizeof(zNm),zNm,"b%c",'a'+i);
465485
zReq = PD(zNm,azDflt[i]);
466486
if( zReq==0 || zReq[0]==0 ) continue;
467487
if( cnt==0 ){
468488
@ <table border="1" cellspacing="0" cellpadding="10">
469489
@ <tr>
@@ -509,14 +529,14 @@
509529
@ <hr>
510530
}
511531
@ <form method="POST">
512532
@ <p>Enter CSS color names below and see them shifted into corresponding
513533
@ background colors above.</p>
514
- for(i=0; i<10; i++){
515
- sqlite3_snprintf(sizeof(zNm),zNm,"b%d",i);
534
+ for(i=0; i<N; i++){
535
+ sqlite3_snprintf(sizeof(zNm),zNm,"b%c",'a'+i);
516536
@ <input type="text" size="30" name='%s(zNm)' \
517537
@ value='%h(PD(zNm,azDflt[i]))'><br>
518538
}
519539
@ <input type="submit" value="Submit">
520540
@ </form>
521541
style_finish_page();
522542
}
523543
--- src/color.c
+++ src/color.c
@@ -259,19 +259,38 @@
259 if( iRGB<0 ) return 0;
260 r = (iRGB>>16) & 0xff;
261 g = (iRGB>>8) & 0xff;
262 b = iRGB & 0xff;
263 if( fg==1 ){
264 const int K = 70;
265 r = (K*r)/255 + (255-K);
266 g = (K*g)/255 + (255-K);
267 b = (K*b)/255 + (255-K);
 
 
 
 
 
 
 
 
 
 
268 }else{
269 const int K = 90;
270 r = (K*r)/255;
271 g = (K*g)/255;
272 b = (K*b)/255;
 
 
 
 
 
 
 
 
 
273 }
274 sqlite3_snprintf(8, zColor, "#%02x%02x%02x", r,g,b);
275 return zColor;
276 }
277
@@ -451,19 +470,20 @@
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>
@@ -509,14 +529,14 @@
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
--- src/color.c
+++ src/color.c
@@ -259,19 +259,38 @@
259 if( iRGB<0 ) return 0;
260 r = (iRGB>>16) & 0xff;
261 g = (iRGB>>8) & 0xff;
262 b = iRGB & 0xff;
263 if( fg==1 ){
264 /* Dark text on a light background. Adjust so that
265 ** no color component is less than 255-K, resulting in
266 ** a pastel background color. Color adjustment is quadratic
267 ** so that colors that are further out of range have a greater
268 ** adjustment. */
269 const int K = 79;
270 int k, x, m;
271 m = r<g ? r : g;
272 if( m>b ) m = b;
273 k = (m*m)/255 + K;
274 x = 255 - k;
275 r = (k*r)/255 + x;
276 g = (k*g)/255 + x;
277 b = (k*b)/255 + x;
278 }else{
279 /* Light text on a dark background. Adjust so that
280 ** no color component is greater than K, resulting in
281 ** a low-intensity, low-saturation background color.
282 ** The color adjustment is quadratic so that colors that
283 ** are further out of range have a greater adjustment. */
284 const int K = 112;
285 int k, m;
286 m = r>g ? r : g;
287 if( m<b ) m = b;
288 k = 255 - (255-K)*(m*m)/65025;
289 r = (k*r)/255;
290 g = (k*g)/255;
291 b = (k*b)/255;
292 }
293 sqlite3_snprintf(8, zColor, "#%02x%02x%02x", r,g,b);
294 return zColor;
295 }
296
@@ -451,19 +470,20 @@
470 const char *zBG; /* Actual color provided */
471 const char *zBg1;
472 char zNm[10];
473 static const char *azDflt[] = {
474 "red", "orange", "yellow", "green", "blue", "indigo", "violet",
475 "tan", "brown", "gray",
476 };
477 const int N = count(azDflt);
478 int i, cnt, iClr, r, g, b;
479 char *zFg;
480 login_check_credentials();
481 style_set_current_feature("test");
482 style_header("Background Color Test");
483 for(i=cnt=0; i<N; i++){
484 sqlite3_snprintf(sizeof(zNm),zNm,"b%c",'a'+i);
485 zReq = PD(zNm,azDflt[i]);
486 if( zReq==0 || zReq[0]==0 ) continue;
487 if( cnt==0 ){
488 @ <table border="1" cellspacing="0" cellpadding="10">
489 @ <tr>
@@ -509,14 +529,14 @@
529 @ <hr>
530 }
531 @ <form method="POST">
532 @ <p>Enter CSS color names below and see them shifted into corresponding
533 @ background colors above.</p>
534 for(i=0; i<N; i++){
535 sqlite3_snprintf(sizeof(zNm),zNm,"b%c",'a'+i);
536 @ <input type="text" size="30" name='%s(zNm)' \
537 @ value='%h(PD(zNm,azDflt[i]))'><br>
538 }
539 @ <input type="submit" value="Submit">
540 @ </form>
541 style_finish_page();
542 }
543

Keyboard Shortcuts

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