Skip to content

Commit a754020

Browse files
committed
c4 theme
1 parent 60ed1dc commit a754020

File tree

12 files changed

+3914
-1
lines changed

12 files changed

+3914
-1
lines changed

d2exporter/export.go

+61
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,46 @@ func applyTheme(shape *d2target.Shape, obj *d2graph.Object, theme *d2themes.Them
7979
if theme.SpecialRules.Mono {
8080
shape.FontFamily = "mono"
8181
}
82+
if theme.SpecialRules.C4 && len(obj.ChildrenArray) > 0 {
83+
if obj.Style.Fill == nil {
84+
shape.Fill = "transparent"
85+
}
86+
if obj.Style.Stroke == nil {
87+
shape.Stroke = color.AA2
88+
}
89+
if obj.Style.StrokeDash == nil {
90+
shape.StrokeDash = 5
91+
}
92+
if obj.Style.FontColor == nil {
93+
shape.Color = color.N1
94+
}
95+
}
96+
}
97+
if theme.SpecialRules.C4 && obj.Level() == 1 && len(obj.ChildrenArray) == 0 &&
98+
obj.Shape.Value != d2target.ShapePerson && obj.Shape.Value != d2target.ShapeC4Person {
99+
if obj.Style.Fill == nil {
100+
shape.Fill = color.B6
101+
}
102+
if obj.Style.Stroke == nil {
103+
shape.Stroke = color.B5
104+
}
105+
}
106+
if theme.SpecialRules.C4 && (obj.Shape.Value == d2target.ShapePerson || obj.Shape.Value == d2target.ShapeC4Person) {
107+
if obj.Style.Fill == nil {
108+
shape.Fill = color.B2
109+
}
110+
if obj.Style.Stroke == nil {
111+
shape.Stroke = color.B1
112+
}
113+
}
114+
if theme.SpecialRules.C4 && obj.Level() > 1 && len(obj.ChildrenArray) == 0 &&
115+
obj.Shape.Value != d2target.ShapePerson && obj.Shape.Value != d2target.ShapeC4Person {
116+
if obj.Style.Fill == nil {
117+
shape.Fill = color.B4
118+
}
119+
if obj.Style.Stroke == nil {
120+
shape.Stroke = color.B3
121+
}
82122
}
83123
}
84124

@@ -165,6 +205,15 @@ func toShape(obj *d2graph.Object, g *d2graph.Graph) d2target.Shape {
165205
applyStyles(shape, obj)
166206
applyTheme(shape, obj, g.Theme)
167207
shape.Color = text.GetColor(shape.Italic)
208+
if g.Theme.SpecialRules.C4 {
209+
if obj.Style.FontColor == nil {
210+
if len(obj.ChildrenArray) > 0 {
211+
shape.Color = color.N1
212+
} else {
213+
shape.Color = color.N7
214+
}
215+
}
216+
}
168217
applyStyles(shape, obj)
169218

170219
switch strings.ToLower(obj.Shape.Value) {
@@ -393,5 +442,17 @@ func toConnection(edge *d2graph.Edge, theme *d2themes.Theme) d2target.Connection
393442
connection.Src = edge.Src.AbsID()
394443
connection.Dst = edge.Dst.AbsID()
395444

445+
if theme != nil && theme.SpecialRules.C4 {
446+
if edge.Style.StrokeDash == nil {
447+
connection.StrokeDash = 5
448+
}
449+
if edge.Style.Stroke == nil {
450+
connection.Stroke = color.AA4
451+
}
452+
if edge.Style.FontColor == nil {
453+
connection.Color = color.N2
454+
}
455+
}
456+
396457
return *connection
397458
}

d2graph/d2graph.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ func (obj *Object) GetFill() string {
565565
return color.AB5
566566
}
567567

568-
if strings.EqualFold(shape, d2target.ShapePerson) {
568+
if strings.EqualFold(shape, d2target.ShapePerson) || strings.EqualFold(shape, d2target.ShapeC4Person) {
569569
return color.B3
570570
}
571571
if strings.EqualFold(shape, d2target.ShapeDiamond) {

d2renderers/d2svg/d2svg.go

+2
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,8 @@ func drawShape(writer, appendixWriter io.Writer, diagramHash string, targetShape
14831483
}
14841484
if !color.IsThemeColor(targetShape.Color) {
14851485
styles = append(styles, fmt.Sprintf(`color:%s`, targetShape.Color))
1486+
} else {
1487+
styles = append(styles, fmt.Sprintf(`color:%s`, d2themes.ResolveThemeColor(*inlineTheme, targetShape.Color)))
14861488
}
14871489

14881490
mdEl.Style = strings.Join(styles, ";")

d2target/d2target.go

+7
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,13 @@ func (diagram Diagram) BoundingBox() (topLeft, bottomRight Point) {
280280
x2 = go2.Max(x2, targetShape.Pos.X+targetShape.Width+int(math.Ceil(float64(targetShape.StrokeWidth)/2.)))
281281
y2 = go2.Max(y2, targetShape.Pos.Y+targetShape.Height+int(math.Ceil(float64(targetShape.StrokeWidth)/2.)))
282282

283+
if targetShape.Type == ShapeC4Person {
284+
headRadius := int(float64(targetShape.Width) * 0.22)
285+
headCenterY := int(float64(targetShape.Height) * 0.18)
286+
headTop := targetShape.Pos.Y + headCenterY - headRadius
287+
y1 = go2.Min(y1, headTop-targetShape.StrokeWidth)
288+
}
289+
283290
if targetShape.Tooltip != "" || targetShape.Link != "" {
284291
// 16 is the icon radius
285292
y1 = go2.Min(y1, targetShape.Pos.Y-targetShape.StrokeWidth-16)

d2themes/d2themes.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type SpecialRules struct {
2121
OuterContainerDoubleBorder bool `json:"outerContainerDoubleBorder"`
2222
ContainerDots bool `json:"containerDots"`
2323
CapsLock bool `json:"capsLock"`
24+
C4 bool `json:"c4"`
2425

2526
AllPaper bool `json:"allPaper"`
2627
}

d2themes/d2themescatalog/c4.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package d2themescatalog
2+
3+
import "oss.terrastruct.com/d2/d2themes"
4+
5+
var C4 = d2themes.Theme{
6+
ID: 303,
7+
Name: "C4",
8+
Colors: d2themes.ColorPalette{
9+
Neutrals: d2themes.Neutral{
10+
N1: "#0f5eaa", // Container font color
11+
N2: "#707070", // Connection font color
12+
N3: "#FFFFFF",
13+
N4: "#073b6f", // Person stroke
14+
N5: "#999999", // Root level objects
15+
N6: "#FFFFFF",
16+
N7: "#FFFFFF",
17+
},
18+
19+
// Primary colors
20+
B1: "#073b6f", // Person stroke
21+
B2: "#08427b", // Person fill
22+
B3: "#3c7fc0", // Inner objects stroke
23+
B4: "#438dd5", // Inner objects fill
24+
B5: "#8a8a8a", // Root level objects stroke
25+
B6: "#999999", // Root level objects fill
26+
27+
// Accent colors
28+
AA2: "#0f5eaa", // Container stroke
29+
AA4: "#707070", // Connection stroke
30+
AA5: "#f5f5f5", // Light background
31+
32+
AB4: "#e1e1e1",
33+
AB5: "#f0f0f0",
34+
},
35+
SpecialRules: d2themes.SpecialRules{
36+
C4: true,
37+
},
38+
}

d2themes/d2themescatalog/catalog.go

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var LightCatalog = []d2themes.Theme{
2525
Terminal,
2626
TerminalGrayscale,
2727
Origami,
28+
C4,
2829
}
2930

3031
var DarkCatalog = []d2themes.Theme{

0 commit comments

Comments
 (0)