Skip to content

Commit 35fb2a4

Browse files
it works!!! and with es6
1 parent 4303c88 commit 35fb2a4

File tree

4 files changed

+83
-79
lines changed

4 files changed

+83
-79
lines changed

index.js

+51-35
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,72 @@
11
"use strict";
22

3-
const valueParser = require("postcss-value-parser");
3+
import valueParser from "postcss-value-parser";
44

5-
//const { gettxt } = require("./tools.mjs");
5+
import { gettxt } from "./tools.mjs";
66

7-
const DEFAULT_OPTS = {
7+
const OPTIONS = {
88
rootFontSize: "16px",
99
fontSize: "16px",
1010
fontWeight: 400,
1111
usage: 4,
1212
};
1313

14+
function plugin(options = {}) {
15+
const { rootFontSize, fontSize, fontWeight, usage, } = Object.assign(OPTIONS, options);
16+
17+
let curM = OPTIONS.rootM = getFontSizePX(rootFontSize);
18+
19+
return {
20+
postcssPlugin: "postcss-accessibility",
21+
22+
Declaration(decl) {
23+
const values = decl.value.split(/(?!\(.*)\s(?![^(]*?\))/g);
24+
25+
for (const valueIndex in values) {
26+
const value = values[valueIndex];
27+
if (value.startsWith("a11y-txt(")) {
28+
const params = valueParser(value).nodes[0].nodes
29+
.filter(i => i.type !== "div")
30+
.map(i => {
31+
switch (i.type) {
32+
case "word": return i.value;
33+
case "function": return `${i.value}(${i.nodes.filter(i => i.type !== "div").map(i => i.value).join("")})`;
34+
}
35+
});
36+
decl.value = gettxt(params[0], "txt", `${getFontSizePX(params[1] ?? fontSize, curM)}px`, params[2] ?? fontWeight, params[3] ?? usage);
37+
}
38+
}
39+
}
40+
}
41+
}
42+
plugin.postcss = true;
43+
1444
/**
1545
* @type {import('postcss').PluginCreator}
1646
*/
17-
module.exports = (opts = {}) => {
18-
const { rootFontSize, fontSize, fontWeight, usage, } = Object.assign(DEFAULT_OPTS, opts);
47+
export default plugin;
48+
49+
function getFontSizePX(fontSize, M = OPTIONS.rootM) {
50+
if (typeof fontSize !== "string" && !(fontSize instanceof String)) return;
1951

20-
if (typeof rootFontSize !== "string" && !(rootFontSize instanceof String)) return;
52+
let [, value, unit] = fontSize.match(/^(\d*\.?\d+)(px|cm|mm|Q|in|pc|pt|rem|em)$/) ?? [];
2153

22-
let [, rootFontSizeValue, rootFontSizeUnit] = rootFontSize.match(/^(\d*\.?\d+)(px|cm|mm|Q|in|pc|pt)$/) ?? [];
54+
if (unit.includes("em") && !OPTIONS.rootM) return;
2355

24-
if (rootFontSizeValue == null || !rootFontSizeUnit == null) return;
56+
if (value == null || !unit == null) return;
2557

26-
if (rootFontSizeUnit.startsWith(".")) rootFontSizeUnit = 0 + rootFontSizeUnit;
58+
if (unit.startsWith(".")) unit = 0 + unit;
2759

28-
switch (rootFontSizeUnit) {
29-
case "px": break;
30-
case "cm": rootFontSizeValue *= 96/2.54; break;
31-
case "mm": rootFontSizeValue *= 96/2.54/10; break;
32-
case "Q": rootFontSizeValue *= 96/2.54/10*4; break;
33-
case "in": rootFontSizeValue *= 96; break;
34-
case "pc": rootFontSizeValue *= 96/6; break;
35-
case "pt": rootFontSizeValue *= 96/72; break;
60+
switch (unit) {
61+
case "em": return value * M;
62+
case "rem": return value * OPTIONS.rootM;
63+
case "px": return value;
64+
case "cm": return value * 96/2.54;
65+
case "mm": return value * 96/2.54/10;
66+
case "Q": return value * 96/2.54/10*4;
67+
case "in": return value * 96;
68+
case "pc": return value * 96/6;
69+
case "pt": return value * 96/72;
3670
default: return;
3771
}
38-
39-
console.log("fs: ", rootFontSizeValue);
40-
41-
// Work with options here
42-
return {
43-
postcssPlugin: 'postcss-accessibility',
44-
//Rule: rule => {
45-
// console.log(rule);
46-
//},
47-
Declaration: decl => {
48-
if (decl.value.startsWith("a11y-txt(")) {
49-
const params = valueParser(decl.value).nodes[0].nodes;
50-
//console.log(/*gettxt(*/params[0].value, "txt", ...Object.values(opts)/*)*/);
51-
}
52-
},
53-
};
5472
}
55-
56-
module.exports.postcss = true;

index.test.js

+9-35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const postcss = require("postcss");
1+
import postcss from "postcss";
22

3-
const plugin = require("./index.js");
3+
import plugin from "./index.js";
44

55
async function run(input, output, opts = { }) {
66
const result = await postcss([plugin(opts)]).process(input, { from: undefined });
@@ -11,40 +11,14 @@ async function run(input, output, opts = { }) {
1111

1212
// Write tests here
1313

14-
it('does something', async () => {
14+
it("finds the best text color for the background-color: hsl(192deg, 62%, 94%)", async () => {
1515
await run(`p {
16-
background-color: #fff;
17-
color: a11y-txt(#fff);
16+
background-color: hsl(192 62% 94%);
17+
color: a11y-txt(hsl(192 62% 94%), 1.75em, 400);
18+
font-size: 1.75em;
1819
}`, `p {
19-
background-color: #fff;
20-
color: a11y-txt(#fff);
20+
background-color: hsl(192 62% 94%);
21+
color: hsl(192.63157894736838 61.290322580645174% 17%);
22+
font-size: 1.75em;
2123
}`, {});
2224
});
23-
it('does something', async () => {
24-
await run(`p {
25-
background-color: #fff;
26-
color: a11y-txt(#fff);
27-
}`, `p {
28-
background-color: #fff;
29-
color: a11y-txt(#fff);
30-
}`, {rootFontSize: ".5in"});
31-
});
32-
it('does something', async () => {
33-
await run(`p {
34-
background-color: #fff;
35-
color: a11y-txt(#fff);
36-
}`, `p {
37-
background-color: #fff;
38-
color: a11y-txt(#fff);
39-
}`, {rootFontSize: "3mm"});
40-
});
41-
it('does something', async () => {
42-
await run(`p {
43-
background-color: #fff;
44-
color: a11y-txt(#fff);
45-
}`, `p {
46-
background-color: #fff;
47-
color: a11y-txt(#fff);
48-
}`, {rootFontSize: "1.5pt"});
49-
});
50-
//color: hsl(0 0% 29%);

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@
5656
"apca-w3": "^0.1.9",
5757
"colorparsley": "^0.1.8",
5858
"postcss-value-parser": "^4.2.0"
59-
}
59+
},
60+
"type": "module"
6061
}

tools.mjs

+21-8
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,35 @@ export function gettxt(color, knownType, fs, fw, usage) {
9898

9999
const { color: contrast } = colors.reduce((x, y) => Math.abs(Math.abs(y.contrast) - target) < Math.abs(Math.abs(x.contrast) - target) ? y : x);
100100

101+
const colors3 = [];
102+
for (let i = 0; i <= 100; i++) {
103+
const newColor = toHSLString({h: 0, s: 0, l: i});
104+
105+
let contrast;
106+
switch (knownType) {
107+
case "txt": contrast = calcAPCA(newColor, color); break;
108+
case "bg": contrast = calcAPCA(color, newColor); break;
109+
}
110+
111+
colors3.push({
112+
color: newColor,
113+
contrast
114+
});
115+
}
116+
117+
const { color: contrast3 } = colors3.reduce((x, y) => Math.abs(Math.abs(y.contrast) - target) < Math.abs(Math.abs(x.contrast) - target) ? y : x);
118+
101119
let contrast2 = reverseAPCA(target, sRGBtoY(colorParsley(color)), knownType, "color");
102120

103121
if (!contrast2) contrast2 = reverseAPCA(-target, sRGBtoY(colorParsley(color)), knownType, "color");
104122

105123
contrast2 = toHSLString(toHSL(contrast2));
106124

107-
console.log(target, contrast, contrast2);
125+
//console.log(target, contrast, contrast2, contrast3);
126+
127+
return contrast;
108128
}
109129

110-
gettxt("hsl(192 63% 94%)", "txt", 48, 700, 4);
111-
gettxt("hsl(192 63% 94%)", "txt", 16, 400, 4);
112-
gettxt("#fff", "txt", 16, 400, 4);
113-
114130
function toHSL(color) {
115131
let [r, g, b] = colorParsley(color);
116132
r /= 255;
@@ -143,6 +159,3 @@ function toHSL(color) {
143159
function toHSLString({h, s, l}) {
144160
return `hsl(${h} ${s} ${l}%)`;
145161
}
146-
147-
toHSL("hsl(192 63% 94%)");
148-
toHSL("rgb(24 98 118)");

0 commit comments

Comments
 (0)