Skip to content

Commit 58a1915

Browse files
authored
Merge pull request #120 from dfpc-coe/circle-color
KML Colors
2 parents f5bb361 + ab2c706 commit 58a1915

7 files changed

Lines changed: 762 additions & 363 deletions

File tree

lib/parser/from_geojson.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,11 @@ export async function from_geojson(
357357
},
358358
Style: {
359359
LineStyle: {
360-
color: { _text: strokeColor.as_hexa().slice(1) },
360+
color: { _text: strokeColor.as_kml() },
361361
width: { _text: cot.event.detail.strokeWeight?._attributes?.value ? cot.event.detail.strokeWeight._attributes.value : 3 }
362362
},
363363
PolyStyle: {
364-
color: { _text: fillColor.as_hexa().slice(1) },
364+
color: { _text: fillColor.as_kml() },
365365
}
366366
}
367367
}

lib/parser/to_geojson.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,27 +376,35 @@ export async function to_geojson(cot: CoT): Promise<Static<typeof Feature>> {
376376
&& raw.event.detail.shape.link?.Style
377377
) {
378378
if (raw.event.detail.shape.link.Style.LineStyle?.color) {
379-
const rawColor = raw.event.detail.shape.link.Style.LineStyle.color._text.startsWith('#')
380-
? raw.event.detail.shape.link.Style.LineStyle.color._text
381-
: '#' + raw.event.detail.shape.link.Style.LineStyle.color._text;
379+
let rawColor = raw.event.detail.shape.link.Style.LineStyle.color._text;
380+
if (rawColor.startsWith('#')) rawColor = rawColor.substring(1);
382381

383-
const strokeColor = new Color(rawColor);
382+
const a = parseInt(rawColor.substring(0, 2), 16);
383+
const b = parseInt(rawColor.substring(2, 4), 16);
384+
const g = parseInt(rawColor.substring(4, 6), 16);
385+
const r = parseInt(rawColor.substring(6, 8), 16);
386+
387+
const strokeColor = new Color([a, r, g, b]);
384388
feat.properties.stroke = strokeColor.as_hex();
385389

386-
feat.properties['stroke-opacity'] = strokeColor.as_opacity();
390+
feat.properties['stroke-opacity'] = strokeColor.as_opacity() / 255;
387391
}
388392

389393
if (raw.event.detail.shape.link.Style.LineStyle?.width) {
390394
feat.properties['stroke-width'] = Number(raw.event.detail.shape.link.Style.LineStyle.width._text);
391395
}
392396

393397
if (raw.event.detail.shape.link.Style.PolyStyle?.color) {
394-
const rawColor = raw.event.detail.shape.link.Style.PolyStyle.color._text.startsWith('#')
395-
? raw.event.detail.shape.link.Style.PolyStyle.color._text
396-
: '#' + raw.event.detail.shape.link.Style.PolyStyle.color._text;
398+
let rawColor = raw.event.detail.shape.link.Style.PolyStyle.color._text;
399+
if (rawColor.startsWith('#')) rawColor = rawColor.substring(1);
400+
401+
const a = parseInt(rawColor.substring(0, 2), 16);
402+
const b = parseInt(rawColor.substring(2, 4), 16);
403+
const g = parseInt(rawColor.substring(4, 6), 16);
404+
const r = parseInt(rawColor.substring(6, 8), 16);
397405

398-
const fillColor = new Color(rawColor);
399-
feat.properties['fill-opacity'] = fillColor.as_opacity();
406+
const fillColor = new Color([a, r, g, b]);
407+
feat.properties['fill-opacity'] = fillColor.as_opacity() / 255;
400408
feat.properties['fill'] = fillColor.as_hex();
401409
}
402410
}

lib/utils/color.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ export default class Color {
5252
}).hexa();
5353
}
5454

55+
as_kml(): string {
56+
const a = Math.round(this.a).toString(16).padStart(2, '0');
57+
const b = Math.round(this.b).toString(16).padStart(2, '0');
58+
const g = Math.round(this.g).toString(16).padStart(2, '0');
59+
const r = Math.round(this.r).toString(16).padStart(2, '0');
60+
61+
return `${a}${b}${g}${r}`.toUpperCase();
62+
}
63+
5564
as_32bit(): number {
5665
return (this.a << 24) | (this.r << 16) | (this.g << 8) | this.b;
5766
}

0 commit comments

Comments
 (0)