improve coloring · boro741/AlgorithmVisualizer@219e343 · GitHub
Skip to content

Commit 219e343

Browse files
committed
improve coloring
1 parent bfb9c3d commit 219e343

7 files changed

Lines changed: 93 additions & 109 deletions

File tree

Lines changed: 56 additions & 55 deletions

js/module/tracer/array2d.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@ class Array2DTracer extends Tracer {
1414
constructor(name) {
1515
super(name);
1616

17-
this.selectColor = '#2962ff';
18-
this.notifyColor = '#c51162';
19-
2017
if (this.isNew) initView(this);
2118
}
2219

2320
_notify(x, y, v) {
2421
this.manager.pushStep(this.capsule, {
2522
type: 'notify',
26-
color: this.notifyColor,
2723
x: x,
2824
y: y,
2925
v: v
@@ -145,8 +141,7 @@ class Array2DTracer extends Tracer {
145141
}
146142
}
147143
var step = {
148-
type: type,
149-
color: this.selectColor
144+
type: type
150145
};
151146
$.extend(step, coord);
152147
this.manager.pushStep(this.capsule, step);
@@ -163,8 +158,8 @@ class Array2DTracer extends Tracer {
163158
case 'denotify':
164159
case 'select':
165160
case 'deselect':
166-
var colorClass = step.color;
167-
var addClass = step.type == 'select' || step.type == 'notify';
161+
var color = step.type == 'select' || step.type == 'deselect' ? this.color.selected : this.color.notified;
162+
var paint = step.type == 'select' || step.type == 'notify';
168163
var sx = step.sx;
169164
var sy = step.sy;
170165
var ex = step.ex;
@@ -173,7 +168,7 @@ class Array2DTracer extends Tracer {
173168
if (sy === undefined) sy = step.y;
174169
if (ex === undefined) ex = step.x;
175170
if (ey === undefined) ey = step.y;
176-
this.paintColor(sx, sy, ex, ey, colorClass, addClass);
171+
this.paintColor(sx, sy, ex, ey, color, paint);
177172
break;
178173
case 'separate':
179174
this.deseparate(step.x, step.y);
@@ -292,22 +287,19 @@ class Array2DTracer extends Tracer {
292287
this.refresh();
293288
}
294289

295-
paintColor(sx, sy, ex, ey, colorClass, addClass) {
290+
paintColor(sx, sy, ex, ey, color, paint) {
296291
for (var i = sx; i <= ex; i++) {
297292
var $row = this.$table.find('.mtbl-row').eq(i);
298293
for (var j = sy; j <= ey; j++) {
299294
var $col = $row.find('.mtbl-col').eq(j);
300-
if(addClass) $col[0].style.backgroundColor = colorClass;
301-
else $col[0].style.backgroundColor = "";
295+
if (paint) $col.css('background', color);
296+
else $col.css('background', '');
302297
}
303298
}
304299
}
305300

306301
clearColor() {
307-
var divs = this.$table.find('.mtbl-col');
308-
for (var i = 0; i < divs.length; i++){
309-
divs[i].style.backgroundColor = "";
310-
}
302+
this.$table.find('.mtbl-col').css('background', '');
311303
}
312304

313305
separate(x, y) {

js/module/tracer/chart.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ class ChartTracer extends Tracer {
1010
constructor(name) {
1111
super(name);
1212

13-
this.selectColor = '#2962ff';
14-
this.notifyColor = '#c51162';
15-
this.defaultColor = 'rgb(136, 136, 136)';
16-
1713
if (this.isNew) initView(this);
1814
}
1915

@@ -25,7 +21,7 @@ class ChartTracer extends Tracer {
2521
}
2622

2723
var color = [];
28-
for (var i = 0; i < C.length; i++) color.push(this.defaultColor);
24+
for (var i = 0; i < C.length; i++) color.push(this.color.default);
2925
this.chart.config.data = {
3026
labels: C.map(String),
3127
datasets: [{
@@ -81,7 +77,7 @@ class ChartTracer extends Tracer {
8177
case 'denotify':
8278
case 'select':
8379
case 'deselect':
84-
let color = step.type == 'notify' ? this.notifyColor : step.type == 'select' ? this.selectColor : this.defaultColor;
80+
let color = step.type == 'notify' ? this.color.notified : step.type == 'select' ? this.color.selected : this.color.default;
8581
if (step.e !== undefined)
8682
for (var i = step.s; i <= step.e; i++)
8783
this.chart.config.data.datasets[0].backgroundColor[i] = color;
@@ -107,7 +103,7 @@ class ChartTracer extends Tracer {
107103
if (data.datasets.length) {
108104
const backgroundColor = data.datasets[0].backgroundColor;
109105
for (let i = 0; i < backgroundColor.length; i++) {
110-
backgroundColor[i] = this.defaultColor;
106+
backgroundColor[i] = this.color.default;
111107
}
112108
this.chart.update();
113109
}

js/module/tracer/coordinate_system.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class CoordinateSystemTracer extends DirectedGraphTracer {
2525
x: C[i][0],
2626
y: C[i][1],
2727
label: '' + i,
28-
size: 1,
29-
color: this.defaultColor
28+
size: 1
3029
});
3130
this.graph.read({
3231
nodes: nodes,
@@ -49,7 +48,7 @@ class CoordinateSystemTracer extends DirectedGraphTracer {
4948
case 'leave':
5049
var visit = step.type == 'visit';
5150
var targetNode = this.graph.nodes(this.n(step.target));
52-
var color = visit ? this.visitedColor : this.leftColor;
51+
var color = visit ? this.color.visited : this.color.left;
5352
targetNode.color = color;
5453
if (step.source !== undefined) {
5554
var edgeId = this.e(step.source, step.target);
@@ -62,7 +61,6 @@ class CoordinateSystemTracer extends DirectedGraphTracer {
6261
id: this.e(step.target, step.source),
6362
source: this.n(step.source),
6463
target: this.n(step.target),
65-
color: color,
6664
size: 1
6765
});
6866
}

js/module/tracer/directed_graph.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ class DirectedGraphTracer extends Tracer {
1414
constructor(name) {
1515
super(name);
1616

17-
this.selectColor = '#2962ff';
18-
this.visitedColor = '#f50057';
19-
this.leftColor = '#616161';
20-
this.defaultColor = '#bdbdbd';
21-
2217
if (this.isNew) initView(this);
2318
}
2419

@@ -57,7 +52,7 @@ class DirectedGraphTracer extends Tracer {
5752
case 'leave':
5853
var visit = step.type == 'visit';
5954
var targetNode = this.graph.nodes(this.n(step.target));
60-
var color = visit ? this.visitedColor : this.leftColor;
55+
var color = visit ? this.color.visited : this.color.left;
6156
targetNode.color = color;
6257
if (step.source !== undefined) {
6358
var edgeId = this.e(step.source, step.target);
@@ -135,7 +130,7 @@ class DirectedGraphTracer extends Tracer {
135130
x: .5 + Math.sin(currentAngle) / 2,
136131
y: .5 + Math.cos(currentAngle) / 2,
137132
size: 1,
138-
color: this.defaultColor,
133+
color: this.color.default,
139134
weight: 0
140135
});
141136

@@ -147,7 +142,7 @@ class DirectedGraphTracer extends Tracer {
147142
id: this.e(i, j),
148143
source: this.n(i),
149144
target: this.n(j),
150-
color: this.defaultColor,
145+
color: this.color.default,
151146
size: 1,
152147
weight: refineByType(value)
153148
});
@@ -160,7 +155,7 @@ class DirectedGraphTracer extends Tracer {
160155
id: this.e(i, j),
161156
source: this.n(i),
162157
target: this.n(j),
163-
color: this.defaultColor,
158+
color: this.color.default,
164159
size: 1,
165160
weight: refineByType(G[i][j])
166161
});
@@ -208,10 +203,10 @@ class DirectedGraphTracer extends Tracer {
208203
var tracer = this;
209204

210205
this.graph.nodes().forEach(function (node) {
211-
node.color = tracer.defaultColor;
206+
node.color = tracer.color.default;
212207
});
213208
this.graph.edges().forEach(function (edge) {
214-
edge.color = tracer.defaultColor;
209+
edge.color = tracer.color.default;
215210
});
216211
}
217212

js/module/tracer/tracer.js

Lines changed: 17 additions & 15 deletions

0 commit comments

Comments
 (0)