-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(colors): adds colors and colors schemes to pattern library
re #147
- Loading branch information
James Nash
committed
Apr 25, 2019
1 parent
0e17a78
commit 3d21734
Showing
21 changed files
with
305 additions
and
389 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const Color = require('color'); | ||
const kebabCase = require('lodash.kebabcase'); | ||
const { colorSchemes } = require('@buildit/gravity-particles'); | ||
|
||
|
||
|
||
function wcagRating(contrastRatio) { | ||
if (contrastRatio > 7) { | ||
return 'AAA'; | ||
} | ||
if (contrastRatio > 4.5) { | ||
return 'AA'; | ||
} | ||
if (contrastRatio > 3) { | ||
return 'A'; | ||
} | ||
return 'Fail'; | ||
} | ||
|
||
function gravityCssCustomPropName(group, colorName) { | ||
return `--grav-co-grp-${group}-${kebabCase(colorName)}`; | ||
} | ||
|
||
|
||
function generateColorSchemeTableData(colorScheme) { | ||
const groupAColorNames = Object.keys(colorScheme.groupA); | ||
const groupBColorNames = Object.keys(colorScheme.groupB); | ||
|
||
const tableData = []; | ||
const headerRow = [null].concat(groupAColorNames.map((name) => gravityCssCustomPropName('a', name))); // top-left cell is empty | ||
tableData.push(headerRow); | ||
|
||
// Loop "down" the rows of the table, one for each group B color | ||
for (let groupBColorName of groupBColorNames) { | ||
const row = [gravityCssCustomPropName('b', groupBColorName)]; // left-most cell has color name | ||
|
||
// Loop "across" the current row, adding one cell for each group A color | ||
for (let groupAColorName of groupAColorNames) { | ||
const colorPair = { | ||
colorA: colorScheme.groupA[groupAColorName], | ||
colorB: colorScheme.groupB[groupBColorName] | ||
}; | ||
|
||
const colorA = new Color(colorPair.colorA); | ||
const colorB = new Color(colorPair.colorB); | ||
|
||
colorPair.contrastRatio = colorA.contrast(colorB); | ||
colorPair.wcagRating = wcagRating(colorPair.contrastRatio); | ||
|
||
row.push(colorPair); | ||
} | ||
|
||
tableData.push(row); | ||
} | ||
|
||
return tableData; | ||
} | ||
|
||
|
||
const colorSchemeTables = {}; | ||
Object.keys(colorSchemes).forEach(colorSchemeName => { | ||
const colorScheme = colorSchemes[colorSchemeName]; | ||
colorSchemeTables[colorSchemeName] = generateColorSchemeTableData(colorScheme); | ||
}); | ||
|
||
module.exports = colorSchemeTables; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/styleguide/_patterns/00-particles/00-color/01-color-palettes.njk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
{% macro colorSwatch(name, value) %} | ||
<div class="sg-color-swatch"> | ||
<div class="sg-color-swatch__preview"> | ||
<span style="background-color: {{ value }};"></span> | ||
</div> | ||
<div class="sg-color-swatch__details"> | ||
<strong>{{ name }}</strong> | ||
<code>{{ value }}</code> | ||
</div> | ||
</div> | ||
{% endmacro -%} | ||
|
||
<p><strong>Note:</strong> The following color palettes are shown for reference only. Color values should never be hard-coded in the SASS/CSS and <code>gravity-ui-web</code>'s SASS code therefore does <em>not</em> provide variables for these colors. Instead, <em>only</em> the CSS custom properties provided Gravity's color system should be referenced when applying colors to UI elements. This ensures that all UI elements work with all available color schemes.</p> | ||
|
||
{%- for brand, palettes in colors %} | ||
<h4>{{ brand }}</h4> | ||
{% for palette, colors in palettes %} | ||
<h5>{{ palette }}</h5> | ||
<div class="sg-color-palette"> | ||
{% for colorName, colorValue in colors %} | ||
{{ colorSwatch(colorName, colorValue) | safe }} | ||
{% endfor %} | ||
</div> | ||
{% endfor %} | ||
{% endfor -%} |
59 changes: 59 additions & 0 deletions
59
src/styleguide/_patterns/00-particles/00-color/02-color-schemes.njk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{%- macro colorProp(varName) %} | ||
<span class="sg-color-prop"> | ||
<span style="background-color: var({{ varName }});"></span> | ||
<code>{{ varName }}</code> | ||
</span> | ||
{% endmacro -%} | ||
|
||
{%- macro colorContrast(contrastRatio, wcagRating, colorA, colorB) %} | ||
<span class="sg-color-contrast"> | ||
<span class="sg-color-contrast__sample" style="color: {{ colorA }}; background-color: {{ colorB }};">Xyz</span> | ||
<span class="sg-color-contrast__sample" style="color: {{ colorB }}; background-color: {{ colorA }};">Xyz</span> | ||
<span class="sg-color-contrast__ratio">{{ contrastRatio | round(3) }}</span> | ||
<span class="sg-color-contrast__score sg-color-contrast__score--{{ wcagRating | lower }}">{{ wcagRating }}</span> | ||
</span> | ||
{% endmacro -%} | ||
|
||
{%- macro colorSchemeTable(tableData) %} | ||
<table> | ||
{% for row in tableData %} | ||
<tr> | ||
{% if loop.first %} | ||
{# header row #} | ||
{% for cell in row %} | ||
{% if loop.first %} | ||
{# top-left cell is blank #} | ||
<td></td> | ||
{% else %} | ||
<th> | ||
{{ colorProp(cell) | safe }} | ||
</th> | ||
{% endif %} | ||
{% endfor %} | ||
{% else %} | ||
{# normal row #} | ||
{% for cell in row %} | ||
{% if loop.first %} | ||
{# first cell is row header #} | ||
<th> | ||
{{ colorProp(cell) | safe }} | ||
</th> | ||
{% else %} | ||
<td> | ||
{{ colorContrast(cell.contrastRatio, cell.wcagRating, cell.colorA, cell.colorB) | safe }} | ||
</td> | ||
{% endif %} | ||
{% endfor %} | ||
{% endif %} | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
{% endmacro -%} | ||
|
||
{% for schemeName, schemeTableData in colorSchemes %} | ||
<section class="sg-color-scheme grav-u-color-scheme-{{ schemeName }}"> | ||
<h4>{{ schemeName | title }} color scheme</h4> | ||
<p>Can be applied to containers using the <code>grav-u-color-scheme-{{ schemeName }}</code> utility class.</p> | ||
{{ colorSchemeTable(schemeTableData) | safe }} | ||
</section> | ||
{% endfor %} |
79 changes: 0 additions & 79 deletions
79
src/styleguide/_patterns/00-particles/00-palettes/01-neutrals.json
This file was deleted.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
src/styleguide/_patterns/00-particles/00-palettes/01-neutrals.njk
This file was deleted.
Oops, something went wrong.
83 changes: 0 additions & 83 deletions
83
src/styleguide/_patterns/00-particles/00-palettes/02-primary-accent.json
This file was deleted.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
src/styleguide/_patterns/00-particles/00-palettes/02-primary-accent.njk
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.