Skip to content

Commit ce08d11

Browse files
committed
Nodes and links are now colored correctly when the background tag is using the format #aarrggbb.
1 parent 39d8a48 commit ce08d11

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Version 1.1.1
44

55
- The width and height from the bounds-attribute on the nodes are now used if the attribute is present.
6+
- Bugfix: Nodes and Edges was not colored correctly when the background tag used the format #aarrggbb.
67

78
## Version 1.1.0
89

src/model/BaseElement.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
export class BaseElement {
3+
4+
public convertColorValue(colorValue: string) {
5+
const regex = /#(\w{2})(\w{2})(\w{2})(\w{2})/i;
6+
const match = regex.exec(colorValue);
7+
if (match) {
8+
let a = this.toNum(match[1]);
9+
console.log(a);
10+
a = a / 255;
11+
return `rgba(${this.toNum(match[2])}, ${this.toNum(match[3])}, ${this.toNum(match[4])}, ${a})`;
12+
} else {
13+
return colorValue;
14+
}
15+
}
16+
private toNum(hex: string): number {
17+
return parseInt(Number('0x' + hex).toString(),10);
18+
}
19+
20+
}

src/model/Link.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ICategory } from '@model';
2+
import { BaseElement } from './BaseElement';
23
// https://schemas.microsoft.com/vs/2009/dgml/dgml.xsd
3-
export class Link {
4+
export class Link extends BaseElement {
45
public source: string | undefined;
56
public target: string | undefined;
67
public category: string | undefined;
@@ -32,8 +33,8 @@ export class Link {
3233
if (this.strokeThickness !== undefined) { jsStringProperties.push(`width: ${this.strokeThickness}`); }
3334
if (this.visibility !== undefined) { jsStringProperties.push(`hidden: ${this.visibility}`); }
3435
const jsStringColorProperties: string[] = [];
35-
if (this.stroke !== undefined) { jsStringColorProperties.push(`color: "${this.stroke}"`); }
36-
if (this.categoryRef !== undefined && this.stroke === undefined && this.categoryRef.background !== undefined) { jsStringProperties.push(`color: "${this.categoryRef.background}"`); }
36+
if (this.stroke !== undefined) { jsStringColorProperties.push(`color: "${this.convertColorValue(this.stroke)}"`); }
37+
if (this.categoryRef !== undefined && this.stroke === undefined && this.categoryRef.background !== undefined) { jsStringProperties.push(`color: "${this.convertColorValue(this.categoryRef.background)}"`); }
3738
if (jsStringColorProperties.length > 0) { jsStringProperties.push(`color: { ${jsStringColorProperties.join(', ')} }`); }
3839
return `{${jsStringProperties.join(', ')}}`;
3940
}

src/model/Node.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { ICategory } from "@model";
2+
import { BaseElement } from "./BaseElement";
23

34
// https://schemas.microsoft.com/vs/2009/dgml/dgml.xsd
4-
export class Node {
5+
export class Node extends BaseElement {
56
public id: string | undefined;
67
public category: string | undefined;
78
public description: string | undefined;
@@ -54,13 +55,13 @@ export class Node {
5455
if (this.strokeThickness !== undefined) { jsStringProperties.push(`borderWidth: "${this.strokeThickness}"`); }
5556
const jsStringColorProperties: string[] = [];
5657
if (this.stroke !== undefined) { jsStringColorProperties.push(`border: "${this.stroke}"`); }
57-
if (this.background !== undefined) { jsStringColorProperties.push(`background: "${this.background}"`); }
58-
if (this.categoryRef !== undefined && this.background === undefined && this.categoryRef.background !== undefined) { jsStringColorProperties.push(`background: "${this.categoryRef.background}"`); }
59-
if (this.categoryRef !== undefined && this.stroke === undefined && this.categoryRef.stroke !== undefined) { jsStringColorProperties.push(`border: "${this.categoryRef.stroke}"`); }
58+
if (this.background !== undefined) { jsStringColorProperties.push(`background: "${this.convertColorValue(this.background)}"`); }
59+
if (this.categoryRef !== undefined && this.background === undefined && this.categoryRef.background !== undefined) { jsStringColorProperties.push(`background: "${this.convertColorValue(this.categoryRef.background)}"`); }
60+
if (this.categoryRef !== undefined && this.stroke === undefined && this.categoryRef.stroke !== undefined) { jsStringColorProperties.push(`border: "${this.convertColorValue(this.categoryRef.stroke)}"`); }
6061
if (jsStringColorProperties.length > 0) { jsStringProperties.push(`color: { ${jsStringColorProperties.join(', ')} }`); }
6162
if (this.boundsX !== undefined && this.boundsY !== undefined) { jsStringProperties.push(`x: ${this.boundsX}, y: ${this.boundsY}, fixed: { x: true, y: true}`); }
62-
if (this.boundsWidth !== undefined) { jsStringProperties.push(`widthConstraint: { minimum: ${this.boundsWidth} }`) }
63-
if (this.boundsHeight !== undefined) { jsStringProperties.push(`heightConstraint: { minimum: ${this.boundsHeight}, valign: top }`) }
63+
if (this.boundsWidth !== undefined) { jsStringProperties.push(`widthConstraint: { minimum: ${this.boundsWidth} }`); }
64+
if (this.boundsHeight !== undefined) { jsStringProperties.push(`heightConstraint: { minimum: ${this.boundsHeight}, valign: top }`); }
6465
return `{${jsStringProperties.join(', ')}}`;
6566
}
6667

0 commit comments

Comments
 (0)