Skip to content

Commit 4303c88

Browse files
adding stuff
1 parent c562ab3 commit 4303c88

File tree

3 files changed

+76
-17
lines changed

3 files changed

+76
-17
lines changed

index.js

+31-8
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,40 @@ const valueParser = require("postcss-value-parser");
44

55
//const { gettxt } = require("./tools.mjs");
66

7-
console.log(valueParser);
8-
//console.log(gettxt);
7+
const DEFAULT_OPTS = {
8+
rootFontSize: "16px",
9+
fontSize: "16px",
10+
fontWeight: 400,
11+
usage: 4,
12+
};
913

1014
/**
1115
* @type {import('postcss').PluginCreator}
1216
*/
13-
module.exports = (opts = {
14-
fontSize: 16,
15-
fontWeight: 400,
16-
usage: 4,
17-
}) => {
17+
module.exports = (opts = {}) => {
18+
const { rootFontSize, fontSize, fontWeight, usage, } = Object.assign(DEFAULT_OPTS, opts);
19+
20+
if (typeof rootFontSize !== "string" && !(rootFontSize instanceof String)) return;
21+
22+
let [, rootFontSizeValue, rootFontSizeUnit] = rootFontSize.match(/^(\d*\.?\d+)(px|cm|mm|Q|in|pc|pt)$/) ?? [];
23+
24+
if (rootFontSizeValue == null || !rootFontSizeUnit == null) return;
25+
26+
if (rootFontSizeUnit.startsWith(".")) rootFontSizeUnit = 0 + rootFontSizeUnit;
27+
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;
36+
default: return;
37+
}
38+
39+
console.log("fs: ", rootFontSizeValue);
40+
1841
// Work with options here
1942
return {
2043
postcssPlugin: 'postcss-accessibility',
@@ -24,7 +47,7 @@ module.exports = (opts = {
2447
Declaration: decl => {
2548
if (decl.value.startsWith("a11y-txt(")) {
2649
const params = valueParser(decl.value).nodes[0].nodes;
27-
console.log(/*gettxt(*/params[0].value, "txt", ...Object.values(opts)/*)*/);
50+
//console.log(/*gettxt(*/params[0].value, "txt", ...Object.values(opts)/*)*/);
2851
}
2952
},
3053
};

index.test.js

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const postcss = require("postcss");
22

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

55
async function run(input, output, opts = { }) {
66
const result = await postcss([plugin(opts)]).process(input, { from: undefined });
7-
console.log("Result CSS: ", result.css);
7+
//console.log("Result CSS: ", result.css);
88
expect(result.css).toEqual(output);
99
expect(result.warnings()).toHaveLength(0);
1010
}
@@ -17,6 +17,34 @@ it('does something', async () => {
1717
color: a11y-txt(#fff);
1818
}`, `p {
1919
background-color: #fff;
20-
color: hsl(0 0% 29%);
20+
color: a11y-txt(#fff);
2121
}`, {});
2222
});
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%);

tools.mjs

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
import { calcAPCA, fontLookupAPCA } from "apca-w3";
3+
import { calcAPCA, fontLookupAPCA, reverseAPCA, sRGBtoY } from "apca-w3";
44
import { colorParsley } from "colorparsley";
55

66
function getFontSize(txtClr, bgClr, fontWeight) {
@@ -11,9 +11,6 @@ function getBoW(txtClr, bgClr) {
1111
return /BoW/.test(calcAPCA(txtClr, bgClr, 0)) ? "#fff" : "#000";
1212
}
1313

14-
//console.log(getFontSize("#113f4b", "#e6f5f9", 500));
15-
//console.log(getBoW("#113f4b", "#e6f5f9"));
16-
1714
// INDEX ARRAYS
1815
// For the following arrays, the Y axis is contrastArrayLen
1916
// The two x axis are weightArrayLen and scoreArrayLen
@@ -85,7 +82,7 @@ export function gettxt(color, knownType, fs, fw, usage) {
8582

8683
const colors = [];
8784
for (let i = 0; i <= 100; i++) {
88-
const newColor = `hsl(${h} ${s} ${i}%)`;
85+
const newColor = toHSLString({h, s, l: i});
8986

9087
let contrast;
9188
switch (knownType) {
@@ -100,7 +97,14 @@ export function gettxt(color, knownType, fs, fw, usage) {
10097
}
10198

10299
const { color: contrast } = colors.reduce((x, y) => Math.abs(Math.abs(y.contrast) - target) < Math.abs(Math.abs(x.contrast) - target) ? y : x);
103-
console.log(target, contrast);
100+
101+
let contrast2 = reverseAPCA(target, sRGBtoY(colorParsley(color)), knownType, "color");
102+
103+
if (!contrast2) contrast2 = reverseAPCA(-target, sRGBtoY(colorParsley(color)), knownType, "color");
104+
105+
contrast2 = toHSLString(toHSL(contrast2));
106+
107+
console.log(target, contrast, contrast2);
104108
}
105109

106110
gettxt("hsl(192 63% 94%)", "txt", 48, 700, 4);
@@ -136,5 +140,9 @@ function toHSL(color) {
136140
return {h, s: s * 100 + "%", l: l * 100 + "%"};
137141
}
138142

143+
function toHSLString({h, s, l}) {
144+
return `hsl(${h} ${s} ${l}%)`;
145+
}
146+
139147
toHSL("hsl(192 63% 94%)");
140148
toHSL("rgb(24 98 118)");

0 commit comments

Comments
 (0)