Browse Source

Proper Station color averaging

Tyler Hallada 6 years ago
parent
commit
7c488cf2ca
5 changed files with 32 additions and 21 deletions
  1. 10 0
      package-lock.json
  2. 3 1
      package.json
  3. 5 2
      src/Station.ts
  4. 14 8
      src/transport.ts
  5. 0 10
      src/utils.ts

+ 10 - 0
package-lock.json

@@ -980,6 +980,11 @@
980 980
       "resolved": "https://registry.npmjs.org/@types/pixi.js/-/pixi.js-4.7.2.tgz",
981 981
       "integrity": "sha512-ybrqVdncNCa81fCYCqxz/CISyMbXl8usszNv0mwdeYDyfDqmemQHJtf4GtduHva+3suhItPc9Akr/WfV19zWiQ=="
982 982
     },
983
+    "@types/tinycolor2": {
984
+      "version": "1.4.0",
985
+      "resolved": "https://registry.npmjs.org/@types/tinycolor2/-/tinycolor2-1.4.0.tgz",
986
+      "integrity": "sha512-cJujCBcb87n9apWNSrUGITAh8cIhpSE62fF4YUtv4tDVrXBNYFGv6bTnkHPSEhjh7TYi01ROY4486iZAcUF4Mg=="
987
+    },
983 988
     "accepts": {
984 989
       "version": "1.3.5",
985 990
       "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.5.tgz",
@@ -11487,6 +11492,11 @@
11487 11492
         "setimmediate": "1.0.5"
11488 11493
       }
11489 11494
     },
11495
+    "tinycolor2": {
11496
+      "version": "1.4.1",
11497
+      "resolved": "https://registry.npmjs.org/tinycolor2/-/tinycolor2-1.4.1.tgz",
11498
+      "integrity": "sha1-9PrTM0R7wLB9TcjpIJ2POaisd+g="
11499
+    },
11490 11500
     "tmp": {
11491 11501
       "version": "0.0.33",
11492 11502
       "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz",

+ 3 - 1
package.json

@@ -43,6 +43,8 @@
43 43
   },
44 44
   "dependencies": {
45 45
     "@types/pixi.js": "^4.7.2",
46
-    "pixi.js": "^4.7.1"
46
+    "@types/tinycolor2": "^1.4.0",
47
+    "pixi.js": "^4.7.1",
48
+    "tinycolor2": "^1.4.1"
47 49
   }
48 50
 }

+ 5 - 2
src/Station.ts

@@ -1,3 +1,5 @@
1
+import * as tinycolor from 'tinycolor2';
2
+
1 3
 import { distance } from './utils';
2 4
 
3 5
 let stationCount = 0;
@@ -30,9 +32,10 @@ export default class Station {
30 32
   public connections: Station[];
31 33
   public id: number;
32 34
   public label: PIXI.Text;
33
-  public color: number;
35
+  public color: tinycolorInstance;
34 36
 
35
-  constructor(location: PIXI.Point, population: number, color: number, connections?: Station[]) {
37
+  constructor(location: PIXI.Point, population: number, color: tinycolorInstance,
38
+              connections?: Station[]) {
36 39
     this.location = location;
37 40
     this.population = population;
38 41
     this.color = color;

+ 14 - 8
src/transport.ts

@@ -1,8 +1,10 @@
1 1
 import * as PIXI from 'pixi.js';
2
+import * as tinycolor from 'tinycolor2';
3
+
2 4
 import Line from './Line';
3 5
 import Station from './Station';
4 6
 import Train from './Train';
5
-import { avgHexColor, distance, pointsAlmostEqual, pointsEqual, randomInt, randomPoint,
7
+import { distance, pointsAlmostEqual, pointsEqual, randomInt, randomPoint,
6 8
          rangeMap, weightedRandom } from './utils';
7 9
 
8 10
 import './style.css';
@@ -44,7 +46,7 @@ const initStations = (numStations: number): Station[] => {
44 46
     stations.push(new Station(
45 47
       randomDistantPoint(stations, 30),
46 48
       randomInt(300, 2000),
47
-      randomInt(0x000000, 0xFFFFFF)));
49
+      tinycolor.random()));
48 50
   }
49 51
   return stations;
50 52
 };
@@ -79,12 +81,16 @@ const moveTrains = (trains: Train[], stations: Station[]) => {
79 81
     // train reached destination, stop moving and let passengers off
80 82
     if (pointsAlmostEqual(train.location, train.destination.location)) {
81 83
       train.speed = 0;
84
+
85
+      train.destination.color = tinycolor.mix(
86
+        train.destination.color,
87
+        train.origin.color,
88
+        Math.round((train.passengers / train.destination.population) * 100),
89
+      );
90
+
82 91
       train.destination.population += train.passengers;
83 92
       train.passengers = 0;
84 93
 
85
-      // TODO: average colors
86
-      // train.destination.color = avgHexColor(train.origin.color, train.destination.color);
87
-
88 94
       // prepare for next journey
89 95
       train.origin = train.destination;
90 96
       train.destination = undefined;
@@ -112,7 +118,7 @@ const moveTrains = (trains: Train[], stations: Station[]) => {
112 118
 const drawStations = (stations: Station[], graphics: PIXI.Graphics) => {
113 119
   for (const station of stations) {
114 120
     const radius = station.population / 60;
115
-    graphics.beginFill(station.color, 0.5);
121
+    graphics.beginFill(parseInt(station.color.toHex(), 16), 0.5);
116 122
     graphics.drawCircle(station.location.x, station.location.y, radius);
117 123
     graphics.endFill();
118 124
     station.label.x = station.location.x + radius + 1;
@@ -122,7 +128,7 @@ const drawStations = (stations: Station[], graphics: PIXI.Graphics) => {
122 128
 
123 129
 const drawTrains = (trains: Train[], graphics: PIXI.Graphics) => {
124 130
   for (const train of trains) {
125
-    graphics.beginFill(train.origin.color, 0.8);
131
+    graphics.beginFill(parseInt(train.origin.color.toHex(), 16), 0.8);
126 132
     graphics.drawCircle(train.location.x, train.location.y,
127 133
                         rangeMap(train.passengers, 0, TRAIN_CAPACITY, 1, 5));
128 134
     graphics.endFill();
@@ -154,7 +160,7 @@ const run = () => {
154 160
   fpsText.y = 0;
155 161
 
156 162
   const stations = initStations(30);
157
-  const trains = initTrains(100, stations);
163
+  const trains = initTrains(50, stations);
158 164
   // const line = new Line(stations, 10);
159 165
 
160 166
   ticker.stop();

+ 0 - 10
src/utils.ts

@@ -42,13 +42,3 @@ export const rangeMap = (num: number, inMin: number, inMax: number,
42 42
                          outMin: number, outMax: number): number => (
43 43
   (num - inMin) * (outMax - outMin) / (inMax - inMin) + outMin
44 44
 );
45
-
46
-// tslint:disable no-bitwise
47
-export const nthByte = (num: number, n: number): number => ((num >> (8 * n)) & 0xff);
48
-
49
-// FIXME: pretty sure this is wrong, just use a color library instead and then convert to hex
50
-export const avgHexColor = (colorA: number, colorB: number): number => (
51
-  (Math.round((nthByte(colorA, 2) + nthByte(colorB, 2)) / 2) & 0xFF) |
52
-  ((Math.round((nthByte(colorA, 1) + nthByte(colorB, 1)) / 2) & 0xFF) << 8) |
53
-  ((Math.round((nthByte(colorA, 0) + nthByte(colorB, 0)) / 2) & 0x0F) << 16)
54
-);