Compare commits

...

2 commits

Author SHA1 Message Date
RMidhunSuresh
57726ddff8 Convert color code to use es6 module 2022-07-11 12:17:33 +05:30
RMidhunSuresh
9c20b6ecc1 Convert color.js to color.mjs 2022-07-11 12:03:07 +05:30
3 changed files with 13 additions and 14 deletions

View file

@ -13,10 +13,9 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {offColor} from 'off-color';
const offColor = require("off-color").offColor; export function derive(value, operation, argument, isDark) {
module.exports.derive = function (value, operation, argument, isDark) {
const argumentAsNumber = parseInt(argument); const argumentAsNumber = parseInt(argument);
if (isDark) { if (isDark) {
// For dark themes, invert the operation // For dark themes, invert the operation

View file

@ -14,12 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const fs = require("fs"); import {readFileSync, mkdirSync, writeFileSync} from "fs";
const path = require("path"); import {resolve} from "path";
const xxhash = require('xxhashjs'); import {h32} from "xxhashjs";
function createHash(content) { function createHash(content) {
const hasher = new xxhash.h32(0); const hasher = new h32(0);
hasher.update(content); hasher.update(content);
return hasher.digest(); return hasher.digest();
} }
@ -30,8 +30,8 @@ function createHash(content) {
* @param {string} primaryColor Primary color for the new svg * @param {string} primaryColor Primary color for the new svg
* @param {string} secondaryColor Secondary color for the new svg * @param {string} secondaryColor Secondary color for the new svg
*/ */
module.exports.buildColorizedSVG = function (svgLocation, primaryColor, secondaryColor) { export function buildColorizedSVG(svgLocation, primaryColor, secondaryColor) {
const svgCode = fs.readFileSync(svgLocation, { encoding: "utf8"}); const svgCode = readFileSync(svgLocation, { encoding: "utf8"});
let coloredSVGCode = svgCode.replaceAll("#ff00ff", primaryColor); let coloredSVGCode = svgCode.replaceAll("#ff00ff", primaryColor);
coloredSVGCode = coloredSVGCode.replaceAll("#00ffff", secondaryColor); coloredSVGCode = coloredSVGCode.replaceAll("#00ffff", secondaryColor);
if (svgCode === coloredSVGCode) { if (svgCode === coloredSVGCode) {
@ -39,9 +39,9 @@ module.exports.buildColorizedSVG = function (svgLocation, primaryColor, secondar
} }
const fileName = svgLocation.match(/.+[/\\](.+\.svg)/)[1]; const fileName = svgLocation.match(/.+[/\\](.+\.svg)/)[1];
const outputName = `${fileName.substring(0, fileName.length - 4)}-${createHash(coloredSVGCode)}.svg`; const outputName = `${fileName.substring(0, fileName.length - 4)}-${createHash(coloredSVGCode)}.svg`;
const outputPath = path.resolve(__dirname, "../../.tmp"); const outputPath = resolve(__dirname, "../../.tmp");
try { try {
fs.mkdirSync(outputPath); mkdirSync(outputPath);
} }
catch (e) { catch (e) {
if (e.code !== "EEXIST") { if (e.code !== "EEXIST") {
@ -49,6 +49,6 @@ module.exports.buildColorizedSVG = function (svgLocation, primaryColor, secondar
} }
} }
const outputFile = `${outputPath}/${outputName}`; const outputFile = `${outputPath}/${outputName}`;
fs.writeFileSync(outputFile, coloredSVGCode); writeFileSync(outputFile, coloredSVGCode);
return outputFile; return outputFile;
} }

View file

@ -8,8 +8,8 @@ const path = require("path");
const manifest = require("./package.json"); const manifest = require("./package.json");
const version = manifest.version; const version = manifest.version;
const compiledVariables = new Map(); const compiledVariables = new Map();
const derive = require("./scripts/postcss/color").derive; import {buildColorizedSVG as replacer} from "./scripts/postcss/svg-colorizer.mjs";
const replacer = require("./scripts/postcss/svg-colorizer").buildColorizedSVG; import {derive} from "./scripts/postcss/color.mjs";
const commonOptions = { const commonOptions = {
logLevel: "warn", logLevel: "warn",