Fossil SCM

I change the branch colour algorithm, for another one I can understand better. No HSV magic; simply few combinations of RGB values based on hash. This algorithm should give either different or equal colours, and not similar colours. This way I think the hash differences are more likely to give different colours. I had the feeling that we were getting too often too similar colours for our branches, but I can't prove that mathematically.

viriketo 2012-04-22 09:50 annotate_links
Commit 243d99d128f35ce35bd26177830fbe91ab29c233
1 file changed +23 -27
+23 -27
--- src/timeline.c
+++ src/timeline.c
@@ -118,40 +118,36 @@
118118
*/
119119
char *hash_color(const char *z){
120120
int i; /* Loop counter */
121121
unsigned int h = 0; /* Hash on the branch name */
122122
int r, g, b; /* Values for red, green, and blue */
123
- int h1, h2, h3, h4; /* Elements of the hash value */
124
- int mx, mn; /* Components of HSV */
125123
static char zColor[10]; /* The resulting color */
126
- static int ix[2] = {0,0}; /* Color chooser parameters */
127
-
128
- if( ix[0]==0 ){
129
- if( db_get_boolean("white-foreground", 0) ){
130
- ix[0] = 140;
131
- ix[1] = 40;
132
- }else{
133
- ix[0] = 216;
134
- ix[1] = 16;
135
- }
136
- }
137
- for(i=0; z[i]; i++ ){
124
+ static int whitefg = -1;
125
+ int cpc = 4; /* colours per component */
126
+ int cfactor = 128/cpc; /* Factor so n*cpc < 128 */
127
+ int cmin = cfactor - 1; /* Factor so the max component is 127
128
+ and the min is different than the bg */
129
+
130
+ if( whitefg = -1 )
131
+ whitefg = db_get_boolean("white-foreground", 0);
132
+
133
+ /* Calculate the hash based on the branch name */
134
+ for( i=0; z[i]; i++ ){
138135
h = (h<<11) ^ (h<<1) ^ (h>>3) ^ z[i];
139136
}
140
- h1 = h % 6; h /= 6;
141
- h3 = h % 30; h /= 30;
142
- h4 = h % 40; h /= 40;
143
- mx = ix[0] - h3;
144
- mn = mx - h4 - ix[1];
145
- h2 = (h%(mx - mn)) + mn;
146
- switch( h1 ){
147
- case 0: r = mx; g = h2, b = mn; break;
148
- case 1: r = h2; g = mx, b = mn; break;
149
- case 2: r = mn; g = mx, b = h2; break;
150
- case 3: r = mn; g = h2, b = mx; break;
151
- case 4: r = h2; g = mn, b = mx; break;
152
- default: r = mx; g = mn, b = h2; break;
137
+
138
+ /* 4 different random values per component, between 31 and 127 */
139
+ r = cmin + (h % cpc) * cfactor; h /= cpc;
140
+ g = cmin + (h % cpc) * cfactor; h /= cpc;
141
+ b = cmin + (h % cpc) * cfactor; h /= cpc;
142
+
143
+ /* In case of blackfg, get the inverse effect */
144
+ if( !whitefg )
145
+ {
146
+ r = 255 - r;
147
+ g = 255 - g;
148
+ b = 255 - b;
153149
}
154150
sqlite3_snprintf(8, zColor, "#%02x%02x%02x", r,g,b);
155151
return zColor;
156152
}
157153
158154
--- src/timeline.c
+++ src/timeline.c
@@ -118,40 +118,36 @@
118 */
119 char *hash_color(const char *z){
120 int i; /* Loop counter */
121 unsigned int h = 0; /* Hash on the branch name */
122 int r, g, b; /* Values for red, green, and blue */
123 int h1, h2, h3, h4; /* Elements of the hash value */
124 int mx, mn; /* Components of HSV */
125 static char zColor[10]; /* The resulting color */
126 static int ix[2] = {0,0}; /* Color chooser parameters */
127
128 if( ix[0]==0 ){
129 if( db_get_boolean("white-foreground", 0) ){
130 ix[0] = 140;
131 ix[1] = 40;
132 }else{
133 ix[0] = 216;
134 ix[1] = 16;
135 }
136 }
137 for(i=0; z[i]; i++ ){
138 h = (h<<11) ^ (h<<1) ^ (h>>3) ^ z[i];
139 }
140 h1 = h % 6; h /= 6;
141 h3 = h % 30; h /= 30;
142 h4 = h % 40; h /= 40;
143 mx = ix[0] - h3;
144 mn = mx - h4 - ix[1];
145 h2 = (h%(mx - mn)) + mn;
146 switch( h1 ){
147 case 0: r = mx; g = h2, b = mn; break;
148 case 1: r = h2; g = mx, b = mn; break;
149 case 2: r = mn; g = mx, b = h2; break;
150 case 3: r = mn; g = h2, b = mx; break;
151 case 4: r = h2; g = mn, b = mx; break;
152 default: r = mx; g = mn, b = h2; break;
153 }
154 sqlite3_snprintf(8, zColor, "#%02x%02x%02x", r,g,b);
155 return zColor;
156 }
157
158
--- src/timeline.c
+++ src/timeline.c
@@ -118,40 +118,36 @@
118 */
119 char *hash_color(const char *z){
120 int i; /* Loop counter */
121 unsigned int h = 0; /* Hash on the branch name */
122 int r, g, b; /* Values for red, green, and blue */
 
 
123 static char zColor[10]; /* The resulting color */
124 static int whitefg = -1;
125 int cpc = 4; /* colours per component */
126 int cfactor = 128/cpc; /* Factor so n*cpc < 128 */
127 int cmin = cfactor - 1; /* Factor so the max component is 127
128 and the min is different than the bg */
129
130 if( whitefg = -1 )
131 whitefg = db_get_boolean("white-foreground", 0);
132
133 /* Calculate the hash based on the branch name */
134 for( i=0; z[i]; i++ ){
 
135 h = (h<<11) ^ (h<<1) ^ (h>>3) ^ z[i];
136 }
137
138 /* 4 different random values per component, between 31 and 127 */
139 r = cmin + (h % cpc) * cfactor; h /= cpc;
140 g = cmin + (h % cpc) * cfactor; h /= cpc;
141 b = cmin + (h % cpc) * cfactor; h /= cpc;
142
143 /* In case of blackfg, get the inverse effect */
144 if( !whitefg )
145 {
146 r = 255 - r;
147 g = 255 - g;
148 b = 255 - b;
 
149 }
150 sqlite3_snprintf(8, zColor, "#%02x%02x%02x", r,g,b);
151 return zColor;
152 }
153
154

Keyboard Shortcuts

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