Skip to content

Commit 6eaedd3

Browse files
feat: Added Border Radius parameter (#326)
Co-authored-by: Jonah Lawrence <[email protected]>
1 parent b9d75cd commit 6eaedd3

File tree

11 files changed

+176
-41
lines changed

11 files changed

+176
-41
lines changed

README.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,25 @@ The `user` field is the only required option. All other fields are optional.
5858

5959
If the `theme` parameter is specified, any color customizations specified will be applied on top of the theme, overriding the theme's values.
6060

61-
| Parameter | Details | Example |
62-
| :---------------: | :--------------------------------------------: | :---------------------------------------------------------------: |
63-
| `user` | GitHub username to show stats for | `DenverCoder1` |
64-
| `theme` | The theme to apply (Default: `default`) | `dark`, `radical`, etc. [🎨➜](./docs/themes.md) |
65-
| `hide_border` | Make the border transparent (Default: `false`) | `true` or `false` |
66-
| `background` | Background color | **hex code** without `#` or **css color** |
67-
| `border` | Border color | **hex code** without `#` or **css color** |
68-
| `stroke` | Stroke line color between sections | **hex code** without `#` or **css color** |
69-
| `ring` | Color of the ring around the current streak | **hex code** without `#` or **css color** |
70-
| `fire` | Color of the fire in the ring | **hex code** without `#` or **css color** |
71-
| `currStreakNum` | Current streak number | **hex code** without `#` or **css color** |
72-
| `sideNums` | Total and longest streak numbers | **hex code** without `#` or **css color** |
73-
| `currStreakLabel` | Current streak label | **hex code** without `#` or **css color** |
74-
| `sideLabels` | Total and longest streak labels | **hex code** without `#` or **css color** |
75-
| `dates` | Date range text color | **hex code** without `#` or **css color** |
76-
| `date_format` | Date format (Default: `M j[, Y]`) | See note below on [Date Formats](#date-formats) |
77-
| `locale` | Locale to use for labels (Default: `en`) | ISO 639-1 code (See [`translations.php`](./src/translations.php)) |
78-
| `type` | Output format (Default: `svg`) | Current options: `svg`, `png` or `json` |
61+
| Parameter | Details | Example |
62+
| :---------------: | :---------------------------------------------: | :---------------------------------------------------------------: |
63+
| `user` | GitHub username to show stats for | `DenverCoder1` |
64+
| `theme` | The theme to apply (Default: `default`) | `dark`, `radical`, etc. [🎨➜](./docs/themes.md) |
65+
| `hide_border` | Make the border transparent (Default: `false`) | `true` or `false` |
66+
| `border_radius` | Set the roundness of the edges (Default: `4.5`) | Number `0` (sharp corners) to `248` (ellipse) |
67+
| `background` | Background color | **hex code** without `#` or **css color** |
68+
| `border` | Border color | **hex code** without `#` or **css color** |
69+
| `stroke` | Stroke line color between sections | **hex code** without `#` or **css color** |
70+
| `ring` | Color of the ring around the current streak | **hex code** without `#` or **css color** |
71+
| `fire` | Color of the fire in the ring | **hex code** without `#` or **css color** |
72+
| `currStreakNum` | Current streak number | **hex code** without `#` or **css color** |
73+
| `sideNums` | Total and longest streak numbers | **hex code** without `#` or **css color** |
74+
| `currStreakLabel` | Current streak label | **hex code** without `#` or **css color** |
75+
| `sideLabels` | Total and longest streak labels | **hex code** without `#` or **css color** |
76+
| `dates` | Date range text color | **hex code** without `#` or **css color** |
77+
| `date_format` | Date format (Default: `M j[, Y]`) | See note below on [Date Formats](#date-formats) |
78+
| `locale` | Locale to use for labels (Default: `en`) | ISO 639-1 code (See [`translations.php`](./src/translations.php)) |
79+
| `type` | Output format (Default: `svg`) | Current options: `svg`, `png` or `json` |
7980

8081
### Date Formats
8182

src/card.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ function generateCard(array $stats, array $params = null): string
142142
// number formatter
143143
$numFormatter = new NumberFormatter($localeCode, NumberFormatter::DECIMAL);
144144

145+
// read border_radius parameter, default to 4.5 if not set
146+
$borderRadius = $params["border_radius"] ?? "4.5";
147+
145148
// total contributions
146149
$totalContributions = $numFormatter->format($stats["totalContributions"]);
147150
$firstContribution = formatDate($stats["firstContribution"], $dateFormat, $localeCode);
@@ -179,7 +182,7 @@ function generateCard(array $stats, array $params = null): string
179182
</style>
180183
<defs>
181184
<clipPath id='outer_rectangle'>
182-
<rect width='495' height='195'/>
185+
<rect width='495' height='195' rx='{$borderRadius}'/>
183186
</clipPath>
184187
<mask id='mask_out_ring_behind_fire'>
185188
<rect width='495' height='195' fill='white'/>
@@ -188,8 +191,7 @@ function generateCard(array $stats, array $params = null): string
188191
</defs>
189192
<g clip-path='url(#outer_rectangle)'>
190193
<g style='isolation:isolate'>
191-
<path d='M 4.5 0 L 490.5 0 C 492.984 0 495 2.016 495 4.5 L 495 190.5 C 495 192.984 492.984 195 490.5 195 L 4.5 195 C 2.016 195 0 192.984 0 190.5 L 0 4.5 C 0 2.016 2.016 0 4.5 0 Z'
192-
style='stroke: {$theme["border"]}; fill: {$theme["background"]};stroke-miterlimit:10;rx: 4.5;'/>
194+
<rect stroke='{$theme["border"]}' fill='{$theme["background"]}' rx='{$borderRadius}' x='0.5' y='0.5' width='494' height='194'/>
193195
</g>
194196
<g style='isolation:isolate'>
195197
<line x1='330' y1='28' x2='330' y2='170' vector-effect='non-scaling-stroke' stroke-width='1' stroke='{$theme["stroke"]}' stroke-linejoin='miter' stroke-linecap='square' stroke-miterlimit='3'/>
@@ -301,6 +303,9 @@ function generateErrorCard(string $message, array $params = null): string
301303
// get requested theme, use $_REQUEST if no params array specified
302304
$theme = getRequestedTheme($params);
303305

306+
// read border_radius parameter, default to 4.5 if not set
307+
$borderRadius = $params["border_radius"] ?? "4.5";
308+
304309
return "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' style='isolation:isolate' viewBox='0 0 495 195' width='495px' height='195px'>
305310
<style>
306311
a {
@@ -309,13 +314,12 @@ function generateErrorCard(string $message, array $params = null): string
309314
</style>
310315
<defs>
311316
<clipPath id='outer_rectangle'>
312-
<rect width='495' height='195'/>
317+
<rect width='495' height='195' rx='{$borderRadius}'/>
313318
</clipPath>
314319
</defs>
315320
<g clip-path='url(#outer_rectangle)'>
316321
<g style='isolation:isolate'>
317-
<path d='M 4.5 0 L 490.5 0 C 492.984 0 495 2.016 495 4.5 L 495 190.5 C 495 192.984 492.984 195 490.5 195 L 4.5 195 C 2.016 195 0 192.984 0 190.5 L 0 4.5 C 0 2.016 2.016 0 4.5 0 Z'
318-
style='stroke: {$theme["border"]}; fill: {$theme["background"]};stroke-miterlimit:10;rx: 4.5;'/>
322+
<rect stroke='{$theme["border"]}' fill='{$theme["background"]}' rx='{$borderRadius}' x='0.5' y='0.5' width='494' height='194'/>
319323
</g>
320324
<g style='isolation:isolate'>
321325
<!-- Error Label -->
@@ -329,8 +333,8 @@ function generateErrorCard(string $message, array $params = null): string
329333
<!-- mask for background behind face -->
330334
<defs>
331335
<mask id='cut-off-area'>
332-
<rect x='0' y='0' width='500' height='500' fill='white' />
333-
<ellipse cx='247.5' cy='31' rx='13' ry='18'/>
336+
<rect x='0' y='0' width='500' height='500' fill='white' />
337+
<ellipse cx='247.5' cy='31' rx='13' ry='18'/>
334338
</mask>
335339
</defs>
336340
<!-- Sad face -->
@@ -362,7 +366,7 @@ function convertSvgToPng(string $svg): string
362366
$svg = preg_replace("/(<style>\X*?<\/style>)/m", "", $svg);
363367
$svg = preg_replace("/(opacity: 0;)/m", "opacity: 1;", $svg);
364368
$svg = preg_replace("/(animation: fadein.*?;)/m", "opacity: 1;", $svg);
365-
$svg = preg_replace("/(animation: currentstreak.*?;)/m", "font-size: 28px;", $svg);
369+
$svg = preg_replace("/(animation: currstreak.*?;)/m", "font-size: 28px;", $svg);
366370
$svg = preg_replace("/<a \X*?>(\X*?)<\/a>/m", '\1', $svg);
367371

368372
// save svg to random file

src/demo/css/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ h2 {
138138
}
139139

140140
.parameters input[type="text"],
141+
.parameters input[type="number"],
141142
.parameters input.jscolor,
142143
.parameters select {
143144
padding: 10px 14px;

src/demo/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ function gtag() {
103103
<option>false</option>
104104
<option>true</option>
105105
</select>
106+
107+
<label for="border_radius">Border Radius</label>
108+
<input class="param" type="number" id="border_radius" name="border_radius" placeholder="4.5" value="4.5" step="0.1">
106109

107110
<label for="locale">Locale</label>
108111
<select class="param" id="locale" name="locale">

src/demo/js/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const preview = {
1010
hide_border: "false",
1111
date_format: "",
1212
locale: "en",
13+
border_radius: "4.5",
1314
},
1415

1516
/**

tests/RenderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ public function testLocaleRenderDateFormat(): void
8585
$this->assertEquals($expected, $render);
8686
}
8787

88+
/**
89+
* Test border radius
90+
*/
91+
public function testBorderRadius(): void
92+
{
93+
$this->testParams["border_radius"] = "16";
94+
// Check that the card is rendered as expected
95+
$render = generateCard($this->testStats, $this->testParams);
96+
$expected = file_get_contents("tests/expected/test_border_radius_card.svg");
97+
$this->assertEquals($expected, $render);
98+
}
99+
88100
/**
89101
* Test JSON render
90102
*/
Lines changed: 117 additions & 0 deletions
Loading

tests/expected/test_card.svg

Lines changed: 2 additions & 3 deletions
Loading

0 commit comments

Comments
 (0)