styles
This commit is contained in:
52
lib/Color.ts
Normal file
52
lib/Color.ts
Normal file
@ -0,0 +1,52 @@
|
||||
/* eslint-disable no-magic-numbers */
|
||||
import { num_to_hex } from '@scode/encoding-helper';
|
||||
|
||||
export class Color {
|
||||
public static readonly black = new Color (0, 0, 0);
|
||||
public static readonly red = new Color (255, 0, 0);
|
||||
public static readonly green = new Color (0, 255, 0);
|
||||
public static readonly yellow = new Color (255, 255, 0);
|
||||
public static readonly blue = new Color (0, 0, 255);
|
||||
public static readonly magenta = new Color (255, 0, 255);
|
||||
public static readonly cyan = new Color (0, 255, 255);
|
||||
public static readonly white = new Color (255, 255, 255);
|
||||
public static readonly transparent = new Color (0, 0, 0, 0);
|
||||
public static readonly gray = new Color (128, 128, 128);
|
||||
|
||||
private red: number;
|
||||
private green: number;
|
||||
private blue: number;
|
||||
private alpha: number;
|
||||
|
||||
private check_range (n: number): void {
|
||||
if (n < 0 || n > 255)
|
||||
throw new Error ('number out of range');
|
||||
}
|
||||
|
||||
public constructor (red: number, green: number, blue: number, alpha = 255) {
|
||||
this.check_range (red);
|
||||
this.check_range (green);
|
||||
this.check_range (blue);
|
||||
this.check_range (alpha);
|
||||
this.red = red;
|
||||
this.green = green;
|
||||
this.blue = blue;
|
||||
this.alpha = alpha;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
public toString (): string {
|
||||
return `#${num_to_hex (
|
||||
this.red,
|
||||
2
|
||||
)}${num_to_hex (
|
||||
this.green,
|
||||
2
|
||||
)}${num_to_hex (
|
||||
this.blue,
|
||||
2
|
||||
)}${this.alpha === 255
|
||||
? ''
|
||||
: num_to_hex (this.alpha, 2)}`;
|
||||
}
|
||||
}
|
18
lib/Edge.ts
18
lib/Edge.ts
@ -1,6 +1,11 @@
|
||||
import { EdgeStyles } from './Styles';
|
||||
import { Color } from './Color';
|
||||
|
||||
export class Edge {
|
||||
public origin: string;
|
||||
public target: string;
|
||||
public style?: EdgeStyles;
|
||||
public color?: Color;
|
||||
|
||||
public constructor (origin: string, target: string) {
|
||||
this.origin = origin;
|
||||
@ -9,6 +14,17 @@ export class Edge {
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
public toString (): string {
|
||||
return `${this.origin} -> ${this.target}`;
|
||||
const attributes = [];
|
||||
if (this.style)
|
||||
attributes.push ({ name: 'style', value: this.style.toString () });
|
||||
if (this.color)
|
||||
attributes.push ({ name: 'color', value: this.color.toString () });
|
||||
|
||||
const attr_string = ` [${attributes.map ((v) => `${v.name}="${v.value}"`)
|
||||
.join (',')}]`;
|
||||
|
||||
return `${this.origin} -> ${this.target}${attributes.length > 0
|
||||
? attr_string
|
||||
: ''}`;
|
||||
}
|
||||
}
|
||||
|
16
lib/Graph.ts
16
lib/Graph.ts
@ -1,18 +1,30 @@
|
||||
import { Element } from './Element';
|
||||
import { Edge } from './Edge';
|
||||
import { Node } from './Node';
|
||||
import { GraphStyles } from './Styles';
|
||||
import { Color } from './Color';
|
||||
|
||||
export class Graph extends Element {
|
||||
public children: Array<Graph> = [];
|
||||
public nodes: Array<Node> = [];
|
||||
public is_root = false;
|
||||
public edges: Array<Edge> = [];
|
||||
public style?: GraphStyles;
|
||||
public color?: Color;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
public toString (level = 0): string {
|
||||
const header = this.parent
|
||||
? `subgraph cluster_${this.full_name}`
|
||||
: `digraph ${this.full_name}`;
|
||||
const attributes = [];
|
||||
if (this.color)
|
||||
attributes.push ({ name: 'color', value: this.color.toString () });
|
||||
if (this.style)
|
||||
attributes.push ({ name: 'style', value: this.style.toString () });
|
||||
|
||||
let attrs = `\n ${attributes.map ((v) => `${v.name} = ${v.value}`)
|
||||
.join ('\n ')}\n`;
|
||||
let children = `\n ${this.children.map ((c) => c.toString (level + 1))
|
||||
.join ('\n ')}\n`;
|
||||
let nodes = `\n ${this.nodes.map ((c) => c.toString ())
|
||||
@ -20,6 +32,8 @@ export class Graph extends Element {
|
||||
let edges = `\n ${this.edges.map ((c) => c.toString ())
|
||||
.join ('\n ')}\n`;
|
||||
|
||||
if (attrs === '\n \n')
|
||||
attrs = '';
|
||||
if (children === '\n \n')
|
||||
children = '';
|
||||
if (nodes === '\n \n')
|
||||
@ -27,7 +41,7 @@ export class Graph extends Element {
|
||||
if (edges === '\n \n')
|
||||
edges = '';
|
||||
|
||||
return `${header} {${children}${nodes}${edges}}`
|
||||
return `${header} {${attrs}${children}${nodes}${edges}}`
|
||||
.replace (/\n/gu, `\n${' '.repeat (level)}`)
|
||||
.replace (/^\s+$/gmu, '');
|
||||
}
|
||||
|
@ -1,9 +1,13 @@
|
||||
import { Element } from './Element';
|
||||
import { NodeStyles } from './Styles';
|
||||
import { Color } from './Color';
|
||||
|
||||
export class Node extends Element {
|
||||
public label?: string;
|
||||
public is_table = false;
|
||||
public table_contents?: Array<Array<string>>;
|
||||
public style?: NodeStyles;
|
||||
public color?: Color;
|
||||
|
||||
public constructor (name: string, parent?: string, label?: string) {
|
||||
super (name, parent);
|
||||
|
32
lib/Styles.ts
Normal file
32
lib/Styles.ts
Normal file
@ -0,0 +1,32 @@
|
||||
enum EdgeStyles {
|
||||
default = '',
|
||||
solid = 'solid',
|
||||
dashed = 'dashed',
|
||||
dotted='dotted',
|
||||
bold='bold'
|
||||
}
|
||||
|
||||
enum NodeStyles {
|
||||
default = '',
|
||||
solid='solid',
|
||||
dashed='dashed',
|
||||
dotted='dotted',
|
||||
bold='bold',
|
||||
rounded='rounded',
|
||||
diagonals='diagonals',
|
||||
filled='filled',
|
||||
striped='striped',
|
||||
wedged='wedged'
|
||||
}
|
||||
|
||||
enum GraphStyles {
|
||||
solid = 'solid',
|
||||
dashed = 'dashed',
|
||||
dotted = 'dotted',
|
||||
bold = 'bold',
|
||||
rounded = 'rounded',
|
||||
filled = 'filled',
|
||||
striped = 'striped'
|
||||
}
|
||||
|
||||
export { EdgeStyles, NodeStyles, GraphStyles };
|
@ -2,3 +2,5 @@ export { Graph } from './Graph';
|
||||
export { Node } from './Node';
|
||||
export { Element } from './Element';
|
||||
export { Edge } from './Edge';
|
||||
export { Color } from './Color';
|
||||
export { EdgeStyles, NodeStyles, GraphStyles } from './Styles';
|
||||
|
Reference in New Issue
Block a user