@dra2020/dra-types 1.8.142 → 1.8.144

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/colormgr.ts CHANGED
@@ -122,6 +122,10 @@ export let EthnicFewStops = [
122
122
  1.00, //
123
123
  ];
124
124
 
125
+ export const ColorBrewerSchemeStops = [
126
+ 0.00, 0.125, 0.125, 0.25, 0.25, 0.375, 0.375, 0.50, 0.50, 0.625, 0.625, 0.75, 0.75, 0.875, 0.875, 1.00
127
+ ];
128
+
125
129
  export type PaletteDefaults = { [key: string]: string };
126
130
  export const DefaultPaletteDefaults: PaletteDefaults = {
127
131
  partisanScale: 'partisanclassic',
@@ -140,23 +144,28 @@ export function makeStops(stops: number[], colors: string[]): Util.Stop[]
140
144
  return result;
141
145
  }
142
146
 
143
- function partisanStops(stops: number[], pd: PaletteDefaults): Util.Stop[]
147
+ // *************** Color functions ***********************
148
+ // These 3 functions: partisanStops, partisanDistrictStops, ethnicStops, check for Color Brewer schemes
149
+ export function partisanStops(stops: number[], palette: PaletteName): Util.Stop[]
144
150
  {
145
- const palette: PaletteName = pd['partisanScale'] as PaletteName;
151
+ if (Colors.BigPalettes.indexOf(palette) < 0) // not a big palette
152
+ stops = ColorBrewerSchemeStops;
146
153
  return makeStops(stops, colorsFromStopsPartisan(palette, 'partisanScale', stops));
147
154
  }
148
155
 
149
- function partisanDistrictStops(stops: number[], pd: PaletteDefaults): Util.Stop[]
156
+ export function partisanDistrictStops(stops: number[], palette: PaletteName): Util.Stop[]
150
157
  {
151
- const palette: PaletteName = pd['partisanDistrictsScale'] as PaletteName;
158
+ if (Colors.BigPalettes.indexOf(palette) < 0) // not a big palette
159
+ stops = ColorBrewerSchemeStops;
152
160
  return makeStops(stops, colorsFromStopsPartisan(palette, 'partisanDistrictsScale', stops));
153
161
  }
154
162
 
155
- function ethnicStops(stops: number[], pd: PaletteDefaults): Util.Stop[]
163
+ export function ethnicStops(stops: number[], palette: PaletteName): Util.Stop[]
156
164
  {
157
- const palette: PaletteName = pd['demographicsScale'] as PaletteName;
158
165
  if (palette === 'demographicsclassic')
159
166
  return makeStops(stops, Colors.EthnicFewClassicColors);
167
+ if (Colors.BigPalettes.indexOf(palette) < 0) // not a big palette
168
+ stops = ColorBrewerSchemeStops;
160
169
  return makeStops(stops, colorsFromStops(palette, stops, Colors.EthnicFewClassicColors));
161
170
  }
162
171
 
@@ -179,12 +188,12 @@ export function ToAllEthnicColor(agg: PF.PackedFields, dc: PF.DatasetContext, pd
179
188
 
180
189
  export function ToPartisanColorStr(agg: PF.PackedFields, dc: PF.DatasetContext, pd: PaletteDefaults): string
181
190
  {
182
- return ToPartisanColor(agg, dc, partisanStops(PartisanPrecinctStops, pd));
191
+ return ToPartisanColor(agg, dc, partisanStops(PartisanPrecinctStops, pd['partisanScale']));
183
192
  }
184
193
 
185
194
  export function ToPartisanDistrictColor(agg: PF.PackedFields, dc: PF.DatasetContext, pd: PaletteDefaults): string
186
195
  {
187
- return ToPartisanColor(agg, dc, partisanDistrictStops(PartisanDistrictStops, pd));
196
+ return ToPartisanColor(agg, dc, partisanDistrictStops(PartisanDistrictStops, pd['partisanDistrictsScale']));
188
197
  }
189
198
 
190
199
  function ToPartisanColor(agg: PF.PackedFields, dc: PF.DatasetContext, stops: Util.GradientStops): string
@@ -206,7 +215,7 @@ export function ToPartisanShiftColor(agg: PF.PackedFields, dc: PF.DatasetContext
206
215
 
207
216
  const rep: number = 0.5 - (shift / 2);
208
217
  const dem: number = 0.5 + (shift / 2);
209
- const stops: Util.GradientStops = defaultIsDistrict ? partisanDistrictStops(PartisanDistrictStops, pd) : partisanStops(PartisanPrecinctStops, pd);
218
+ const stops: Util.GradientStops = defaultIsDistrict ? partisanDistrictStops(PartisanDistrictStops, pd['partisanDistrictsScale']) : partisanStops(PartisanPrecinctStops, pd['partisanScale']);
210
219
  const color: string = ColorFromRGBPcts(rep, 0, dem, stops);
211
220
  // console.log('Shift (r, d, color): (' + rep + ', ' + dem + ', ' + color + ')');
212
221
  return color;
@@ -246,7 +255,7 @@ export function ToEthnicColorStr(agg: PF.PackedFields, dc: PF.DatasetContext, pd
246
255
  if (den == 0)
247
256
  return '#ffffff';
248
257
  const pct = bInvert ? 1 - (num / den) : num / den;
249
- return Util.execGradient(ethnicStops(EthnicFewStops, pd), pct);
258
+ return Util.execGradient(ethnicStops(EthnicFewStops, pd['demographicsScale']), pct);
250
259
  }
251
260
 
252
261
  // All Groups Mosaic
@@ -377,15 +386,22 @@ export function ColorFromRGBPcts(pctRed: number, pctGreen: number, pctBlue: numb
377
386
 
378
387
  export type ColorUse = 'districts' | 'partisanScale' | 'demographicsScale' | 'demographicsAllGroups' | 'partisanDistrictsScale';
379
388
 
380
- // Currently supported palettes
389
+ // Currently supported palettes (not including Color Brewer schemes)
381
390
  export const PaletteNames = ['jet_r', 'turbo_r', 'inferno_r', 'viridis_r', 'magma_r', 'plasma_r', 'Greys', 'bone_r',
382
391
  'draclassic', 'demographicsclassic', 'partisanclassic', 'allgroupsclassic', 'partisandistrictsclassic'] as const;
383
- export type PaletteName = typeof PaletteNames[number];
392
+ export type PaletteName = string; //typeof PaletteNames[number]; // This can be a base palette name or the name of a color scheme
384
393
 
385
- export function colorsFromStops(palette: PaletteName, stops: number[], classicColors: string[]): string[]
394
+ function colorsFromStops(palette: PaletteName, stops: number[], classicColors: string[]): string[]
386
395
  {
387
- // Use classicColors to see where there are duplicate colors
388
396
  const allColors: string[] = Colors.getPalette(palette);
397
+ if (allColors.length < Colors.MaxColors)
398
+ {
399
+ if (allColors.length != 8) // Color Brewer Schemes are only 8 colors
400
+ return classicColors; // Safety check; shouldn't happen
401
+ return colorBrewerColors(stops, allColors);
402
+ }
403
+
404
+ // Use classicColors to see where there are duplicate colors
389
405
  let colors: string[] = [];
390
406
  for (let i = 0; i < stops.length; i++)
391
407
  {
@@ -400,7 +416,7 @@ export function colorsFromStops(palette: PaletteName, stops: number[], classicCo
400
416
  return colors;
401
417
  }
402
418
 
403
- export function colorsFromStopsPartisan(palette: PaletteName, colorUse: ColorUse, stops: number[]): string[]
419
+ function colorsFromStopsPartisan(palette: PaletteName, colorUse: ColorUse, stops: number[]): string[]
404
420
  {
405
421
  if (palette === 'partisanclassic')
406
422
  return Colors.PartisanPrecinctClassicColors;
@@ -410,15 +426,31 @@ export function colorsFromStopsPartisan(palette: PaletteName, colorUse: ColorUse
410
426
  // For partisanScale and partisanDistrictsScale; indexes match classic color pattern
411
427
  const allColors: string[] = Colors.getPalette(palette);
412
428
  if (allColors.length < Colors.MaxColors)
413
- return Colors.PartisanPrecinctClassicColors; // Safety check; shouldn't happen
429
+ {
430
+ if (allColors.length != 8) // Color Brewer Schemes are only 8 colors
431
+ return Colors.PartisanPrecinctClassicColors; // Safety check; shouldn't happen
432
+ return colorBrewerColors(stops, allColors);
433
+ }
414
434
 
415
435
  let colors: string[] = [];
416
-
417
436
  const indexes: number[] = colorUse === 'partisanScale' ?
418
437
  [15, 27, 37, 47, 57, 67, 82, 92, 102, 112, 122, 134] : [15, 15, 37, 37, 47, 67, 82, 102, 122, 122, 134, 134];
419
438
  for (let i = 0; i < stops.length; i++)
420
- {
421
439
  colors.push(allColors[indexes[i]]);
440
+
441
+ return colors;
442
+ }
443
+
444
+ function colorBrewerColors(stops: number[], allColors: string[]): string[]
445
+ {
446
+ // Do each color twice (expecting 16 stops)
447
+ const colors: string[] = [];
448
+ for (let i = 0; i < stops.length; i++)
449
+ {
450
+ let colorInx = Math.floor(i / 2);
451
+ if (colorInx >= allColors.length)
452
+ colorInx = allColors.length - 1;
453
+ colors.push(allColors[colorInx]);
422
454
  }
423
455
  return colors;
424
456
  }
package/lib/datasets.ts CHANGED
@@ -118,3 +118,50 @@ export function datasetRestrict(ds: Dataset): { [key: string]: boolean }
118
118
 
119
119
  // Index of database records
120
120
  export type DatasetIndex = { [id: string]: Dataset };
121
+
122
+ export interface PrimaryKeys { CENSUS: string, VAP: string, ELECTION: string };
123
+ export function defaultBuiltinKeys(state: string, datasource: string, planType: string, builtins: Set<string>): PrimaryKeys
124
+ {
125
+ function bestOf(a: string[]): string { return a.find(s => builtins.has(s)) }
126
+
127
+ return (datasource === '2020_VD')
128
+ ? {
129
+ CENSUS: bestOf((usesPrisonerAdjust(state, datasource, planType)
130
+ ? ['D20FA', 'D20F', 'D19F', 'D18F']
131
+ : ['D20F', 'D19F', 'D18F'])),
132
+ VAP: bestOf(['D20T', 'D19T', 'D18T']),
133
+ ELECTION: bestOf(['C16GCO', 'E20GPR', 'E16GPR'])
134
+ }
135
+ :
136
+ {
137
+ CENSUS: bestOf(['D10F', 'D16F']),
138
+ VAP: bestOf(['D10T', 'D16T']),
139
+ ELECTION: bestOf(['C16GCO', 'E20GPR', 'E16GPR', 'E08GPR'])
140
+ }
141
+ }
142
+
143
+ // Hard-coded based on both the existence of the dataset and the legal requirements of that particular state
144
+ // (e.g. NY uses for state house maps but not congressional maps). So that is why we hard-code rather than
145
+ // derive from dataset metadata.
146
+ //
147
+ export function usesPrisonerAdjust(state: string, datasource: string, planType: string): boolean
148
+ {
149
+ if (datasource !== '2020_VD') return false;
150
+ switch (state)
151
+ {
152
+ case 'CA':
153
+ case 'DE':
154
+ case 'MD':
155
+ case 'NJ':
156
+ case 'NV':
157
+ case 'PA':
158
+ case 'VA':
159
+ case 'WA':
160
+ return (planType === 'congress' || planType === 'upper' || planType === 'lower' || planType === 'coi');
161
+ case 'CO':
162
+ case 'CT':
163
+ case 'NY':
164
+ return (planType === 'upper' || planType === 'lower' || planType === 'coi');
165
+ }
166
+ return false;
167
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dra2020/dra-types",
3
- "version": "1.8.142",
3
+ "version": "1.8.144",
4
4
  "description": "Shared types used between client, server and tools.",
5
5
  "main": "dist/dra-types.js",
6
6
  "types": "./dist/all.d.ts",
@@ -36,7 +36,7 @@
36
36
  "webpack-cli": "^5.1.4"
37
37
  },
38
38
  "dependencies": {
39
- "@dra2020/baseclient": "^1.0.158",
39
+ "@dra2020/baseclient": "^1.0.161",
40
40
  "geojson": "^0.5.0",
41
41
  "object-hash": "^3.0.0"
42
42
  }