Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/support for short color notation #194

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ This is how the Decorator pattern can look in nomnoml syntax:
[<hidden> name]
[<table> name| a | 5 || b | 7]

### Add new types
```javascript
const component = {
name: 'newTypeComponentName',
layout: function (config, clas) {
},
visualizer: function (node, x, y, config, g) {
}
}
nomnoml.registerComponent(component);
```
#### Example how to use new type
[<actor>Actor] --> [<newTypeComponentName>Component]

### Directives

#import: my-common-styles.nomnoml
Expand Down
31 changes: 29 additions & 2 deletions src/GraphicsSvg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface SvgAttr {
'stroke-linejoin'?: string
'stroke-linecap'?: string
fill?: string
'fill-opacity'?: string
'text-align'?: string
font?: string
'font-size'?: string
Expand Down Expand Up @@ -164,6 +165,27 @@ export function GraphicsSvg(document?: HTMLDocument): ISvgGraphics {
return element
}

function prepareColor(colorSrc: string): { color: string, opacity: string } {
let color = colorSrc;
let opacity = "1";
if (!colorSrc.match(/^#/)) {
return {
color,
opacity
};
}
const charsQty = colorSrc.slice(1).length;
if (charsQty === 4 || charsQty === 8) {
color = charsQty === 4 ? colorSrc.slice(0,4) : colorSrc.slice(0,7);
opacity = charsQty === 4 ? colorSrc.slice(4, 5).repeat(2) : colorSrc.slice(7, 9);
opacity = String((parseInt(opacity, 16))/255);
}
return {
color,
opacity
}
}

return {
width: function () {
return 0
Expand Down Expand Up @@ -215,7 +237,9 @@ export function GraphicsSvg(document?: HTMLDocument): ISvgGraphics {
current.attr.stroke = stroke
},
fillStyle: function (fill) {
current.attr.fill = fill
const color = prepareColor(fill);
current.attr.fill = color.color
current.attr["fill-opacity"] = color.opacity
},
arcTo: function (x1, y1, x2, y2) {
if (inPathBuilderMode)
Expand All @@ -227,11 +251,14 @@ export function GraphicsSvg(document?: HTMLDocument): ISvgGraphics {
return el('path', { d: '' })
},
fillText: function (text, x, y) {
const color = prepareColor(current.attr.fill!);
return el(
'text',
{
x,
y,
fill: color.color,
'fill-opacity': color.opacity,
stroke: 'none',
font: undefined as undefined | string,
style: undefined as undefined | string,
Expand Down Expand Up @@ -341,4 +368,4 @@ export function GraphicsSvg(document?: HTMLDocument): ISvgGraphics {
return root.serialize()
},
}
}
}
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export {
draw,
renderSvg,
compileFile,
processImports,
processAsyncImports,
ImportDepthError,
draw,
renderSvg,
compileFile,
processImports,
processAsyncImports,
ImportDepthError,
} from './nomnoml'
export var version = '1.5.3'

Expand Down