2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2019-11-09 02:28:11 +08:00
|
|
|
import { Promise } from "rsvp";
|
2016-09-17 04:12:56 +08:00
|
|
|
/*eslint no-bitwise:0 */
|
2016-09-23 02:39:36 +08:00
|
|
|
import getUrl from "discourse-common/lib/get-url";
|
2021-08-26 05:10:12 +08:00
|
|
|
import { htmlSafe } from "@ember/template";
|
2019-11-09 02:28:11 +08:00
|
|
|
import { scheduleOnce } from "@ember/runloop";
|
2022-06-07 03:01:34 +08:00
|
|
|
import { observes } from "discourse-common/utils/decorators";
|
2016-09-17 04:12:56 +08:00
|
|
|
|
2016-09-17 04:39:12 +08:00
|
|
|
export const LOREM = `
|
2018-07-24 21:00:20 +08:00
|
|
|
Lorem ipsum dolor sit amet,
|
|
|
|
consectetur adipiscing elit.
|
|
|
|
Nullam eget sem non elit
|
|
|
|
tincidunt rhoncus. Fusce
|
|
|
|
velit nisl, porttitor sed
|
|
|
|
nisl ac, consectetur interdum
|
|
|
|
metus. Fusce in consequat
|
|
|
|
augue, vel facilisis felis.`;
|
2016-09-17 04:39:12 +08:00
|
|
|
|
2016-09-22 04:48:48 +08:00
|
|
|
const scaled = {};
|
|
|
|
|
|
|
|
function canvasFor(image, w, h) {
|
|
|
|
w = Math.ceil(w);
|
|
|
|
h = Math.ceil(h);
|
|
|
|
|
2019-04-15 14:16:05 +08:00
|
|
|
const scale = window.devicePixelRatio;
|
|
|
|
|
2016-09-22 04:48:48 +08:00
|
|
|
const can = document.createElement("canvas");
|
2019-04-15 14:16:05 +08:00
|
|
|
can.width = w * scale;
|
|
|
|
can.height = h * scale;
|
2016-09-22 04:48:48 +08:00
|
|
|
|
|
|
|
const ctx = can.getContext("2d");
|
2019-04-15 14:16:05 +08:00
|
|
|
ctx.scale(scale, scale);
|
2016-09-22 04:48:48 +08:00
|
|
|
ctx.drawImage(image, 0, 0, w, h);
|
|
|
|
return can;
|
|
|
|
}
|
|
|
|
|
2016-09-17 04:12:56 +08:00
|
|
|
export function createPreviewComponent(width, height, obj) {
|
2019-04-15 14:16:05 +08:00
|
|
|
const scale = window.devicePixelRatio;
|
2019-10-24 00:30:52 +08:00
|
|
|
return Component.extend(
|
2016-09-17 04:12:56 +08:00
|
|
|
{
|
|
|
|
layoutName: "components/theme-preview",
|
|
|
|
width,
|
|
|
|
height,
|
2019-04-15 14:16:05 +08:00
|
|
|
elementWidth: width * scale,
|
|
|
|
elementHeight: height * scale,
|
2021-08-26 05:10:12 +08:00
|
|
|
canvasStyle: htmlSafe(`width:${width}px;height:${height}px`),
|
2016-09-17 04:12:56 +08:00
|
|
|
ctx: null,
|
|
|
|
loaded: false,
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-09-17 04:12:56 +08:00
|
|
|
didInsertElement() {
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2019-07-16 18:45:15 +08:00
|
|
|
const c = this.element.querySelector("canvas");
|
2016-09-17 04:12:56 +08:00
|
|
|
this.ctx = c.getContext("2d");
|
2019-05-08 20:14:28 +08:00
|
|
|
this.ctx.scale(scale, scale);
|
2016-09-17 04:12:56 +08:00
|
|
|
this.reload();
|
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2022-06-07 03:01:34 +08:00
|
|
|
@observes(
|
|
|
|
"step.fieldsById.{color_scheme,body_font,heading_font,homepage_style}.value"
|
|
|
|
)
|
|
|
|
themeChanged() {
|
|
|
|
this.triggerRepaint();
|
|
|
|
},
|
|
|
|
|
2016-09-22 04:09:18 +08:00
|
|
|
images() {},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-08-31 18:14:09 +08:00
|
|
|
loadFonts() {
|
|
|
|
return document.fonts.ready;
|
|
|
|
},
|
|
|
|
|
2016-09-22 04:09:18 +08:00
|
|
|
loadImages() {
|
|
|
|
const images = this.images();
|
|
|
|
if (images) {
|
2019-11-09 02:28:11 +08:00
|
|
|
return Promise.all(
|
2016-09-22 04:09:18 +08:00
|
|
|
Object.keys(images).map((id) => {
|
|
|
|
return loadImage(images[id]).then((img) => (this[id] = img));
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2019-11-09 02:28:11 +08:00
|
|
|
return Promise.resolve();
|
2016-09-22 04:09:18 +08:00
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-09-17 04:12:56 +08:00
|
|
|
reload() {
|
2020-08-31 18:14:09 +08:00
|
|
|
Promise.all([this.loadFonts(), this.loadImages()]).then(() => {
|
2016-09-17 04:12:56 +08:00
|
|
|
this.loaded = true;
|
|
|
|
this.triggerRepaint();
|
|
|
|
});
|
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-09-17 04:12:56 +08:00
|
|
|
triggerRepaint() {
|
2019-10-30 21:48:24 +08:00
|
|
|
scheduleOnce("afterRender", this, "repaint");
|
2016-09-17 04:12:56 +08:00
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-09-17 04:12:56 +08:00
|
|
|
repaint() {
|
|
|
|
if (!this.loaded) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:01:47 +08:00
|
|
|
const colorsArray = this.wizard.currentColors;
|
2021-08-26 05:10:12 +08:00
|
|
|
if (!colorsArray) {
|
2016-09-17 04:12:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-26 05:10:12 +08:00
|
|
|
let colors = {};
|
|
|
|
colorsArray.forEach(function (c) {
|
|
|
|
const name = c.name;
|
|
|
|
colors[name] = `#${c.hex}`;
|
|
|
|
});
|
|
|
|
|
2022-06-07 03:01:47 +08:00
|
|
|
const { font, headingFont } = this.wizard;
|
2020-08-31 18:14:09 +08:00
|
|
|
if (!font) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-17 04:12:56 +08:00
|
|
|
const { ctx } = this;
|
|
|
|
|
|
|
|
ctx.fillStyle = colors.secondary;
|
|
|
|
ctx.fillRect(0, 0, width, height);
|
|
|
|
|
2020-10-06 01:40:41 +08:00
|
|
|
const options = {
|
|
|
|
ctx,
|
|
|
|
colors,
|
|
|
|
font,
|
|
|
|
headingFont,
|
|
|
|
width: this.width,
|
|
|
|
height: this.height,
|
|
|
|
};
|
|
|
|
this.paint(options);
|
2016-09-22 04:09:18 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
categories() {
|
2018-06-14 14:31:56 +08:00
|
|
|
return [
|
|
|
|
{ name: "consecteteur", color: "#652D90" },
|
|
|
|
{ name: "ultrices", color: "#3AB54A" },
|
|
|
|
{ name: "placerat", color: "#25AAE2" },
|
|
|
|
];
|
2016-09-22 04:09:18 +08:00
|
|
|
},
|
|
|
|
|
2016-09-22 04:48:48 +08:00
|
|
|
scaleImage(image, x, y, w, h) {
|
|
|
|
w = Math.floor(w);
|
|
|
|
h = Math.floor(h);
|
|
|
|
|
|
|
|
const { ctx } = this;
|
|
|
|
|
|
|
|
const key = `${image.src}-${w}-${h}`;
|
|
|
|
|
|
|
|
if (!scaled[key]) {
|
|
|
|
let copy = image;
|
|
|
|
let ratio = copy.width / copy.height;
|
|
|
|
let newH = copy.height * 0.5;
|
|
|
|
while (newH > h) {
|
|
|
|
copy = canvasFor(copy, ratio * newH, newH);
|
|
|
|
newH = newH * 0.5;
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2016-09-22 04:48:48 +08:00
|
|
|
|
|
|
|
scaled[key] = copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.drawImage(scaled[key], x, y, w, h);
|
|
|
|
},
|
|
|
|
|
2020-10-06 01:40:41 +08:00
|
|
|
drawFullHeader(colors, font, logo) {
|
2016-09-22 04:48:48 +08:00
|
|
|
const { ctx } = this;
|
|
|
|
|
2016-09-22 04:09:18 +08:00
|
|
|
const headerHeight = height * 0.15;
|
|
|
|
drawHeader(ctx, colors, width, headerHeight);
|
|
|
|
|
|
|
|
const avatarSize = height * 0.1;
|
|
|
|
const headerMargin = headerHeight * 0.2;
|
|
|
|
|
2020-10-14 08:06:09 +08:00
|
|
|
if (logo) {
|
|
|
|
const logoHeight = headerHeight - headerMargin * 2;
|
|
|
|
|
|
|
|
const ratio = logoHeight / logo.height;
|
|
|
|
this.scaleImage(
|
|
|
|
logo,
|
|
|
|
headerMargin,
|
|
|
|
headerMargin,
|
|
|
|
logo.width * ratio,
|
|
|
|
logoHeight
|
|
|
|
);
|
2020-10-06 01:40:41 +08:00
|
|
|
|
2020-10-14 08:06:09 +08:00
|
|
|
this.scaleImage(logo, width, headerMargin);
|
|
|
|
}
|
2016-11-08 00:06:32 +08:00
|
|
|
|
|
|
|
// Top right menu
|
|
|
|
this.scaleImage(
|
|
|
|
this.avatar,
|
|
|
|
width - avatarSize - headerMargin,
|
2016-09-22 04:09:18 +08:00
|
|
|
headerMargin,
|
2016-09-22 04:48:48 +08:00
|
|
|
avatarSize,
|
2016-09-22 04:09:18 +08:00
|
|
|
avatarSize
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
2022-07-27 09:23:01 +08:00
|
|
|
// accounts for hard-set color variables in solarized themes
|
|
|
|
ctx.fillStyle =
|
|
|
|
colors.primary_low_mid ||
|
|
|
|
darkLightDiff(colors.primary, colors.secondary, 45, 55);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2018-12-20 16:25:22 +08:00
|
|
|
const pathScale = headerHeight / 1200;
|
|
|
|
// search icon SVG path
|
|
|
|
const searchIcon = new Path2D(
|
|
|
|
"M505 442.7L405.3 343c-4.5-4.5-10.6-7-17-7H372c27.6-35.3 44-79.7 44-128C416 93.1 322.9 0 208 0S0 93.1 0 208s93.1 208 208 208c48.3 0 92.7-16.4 128-44v16.3c0 6.4 2.5 12.5 7 17l99.7 99.7c9.4 9.4 24.6 9.4 33.9 0l28.3-28.3c9.4-9.4 9.4-24.6.1-34zM208 336c-70.7 0-128-57.2-128-128 0-70.7 57.2-128 128-128 70.7 0 128 57.2 128 128 0 70.7-57.2 128-128 128z"
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
2018-12-20 16:25:22 +08:00
|
|
|
// hamburger icon
|
|
|
|
const hamburgerIcon = new Path2D(
|
|
|
|
"M16 132h416c8.837 0 16-7.163 16-16V76c0-8.837-7.163-16-16-16H16C7.163 60 0 67.163 0 76v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16 16v40c0 8.837 7.163 16 16 16zm0 160h416c8.837 0 16-7.163 16-16v-40c0-8.837-7.163-16-16-16H16c-8.837 0-16 7.163-16 16v40c0 8.837 7.163 16 16 16z"
|
|
|
|
);
|
|
|
|
ctx.save(); // Save the previous state for translation and scale
|
|
|
|
ctx.translate(
|
2016-09-22 04:09:18 +08:00
|
|
|
width - avatarSize * 3 - headerMargin * 0.5,
|
2018-12-20 16:25:22 +08:00
|
|
|
avatarSize / 2
|
|
|
|
);
|
|
|
|
// need to scale paths otherwise they're too large
|
|
|
|
ctx.scale(pathScale, pathScale);
|
|
|
|
ctx.fill(searchIcon);
|
|
|
|
ctx.restore();
|
|
|
|
ctx.save();
|
|
|
|
ctx.translate(
|
|
|
|
width - avatarSize * 2 - headerMargin * 0.5,
|
|
|
|
avatarSize / 2
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
2018-12-20 16:25:22 +08:00
|
|
|
ctx.scale(pathScale, pathScale);
|
|
|
|
ctx.fill(hamburgerIcon);
|
|
|
|
ctx.restore();
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
|
|
|
|
2020-08-31 18:14:09 +08:00
|
|
|
drawPills(colors, font, headerHeight, opts) {
|
2016-09-22 05:34:15 +08:00
|
|
|
opts = opts || {};
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-09-22 04:09:18 +08:00
|
|
|
const { ctx } = this;
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-11-08 00:06:32 +08:00
|
|
|
const categoriesSize = headerHeight * 2;
|
|
|
|
const badgeHeight = categoriesSize * 0.25;
|
2016-09-22 04:09:18 +08:00
|
|
|
const headerMargin = headerHeight * 0.2;
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-11-08 00:06:32 +08:00
|
|
|
ctx.beginPath();
|
2018-12-20 16:25:22 +08:00
|
|
|
ctx.strokeStyle = colors.primary;
|
|
|
|
ctx.lineWidth = 0.5;
|
2018-06-15 23:03:24 +08:00
|
|
|
ctx.rect(
|
2016-09-22 05:34:15 +08:00
|
|
|
headerMargin,
|
2016-11-08 00:06:32 +08:00
|
|
|
headerHeight + headerMargin,
|
2016-09-22 05:34:15 +08:00
|
|
|
categoriesSize,
|
|
|
|
badgeHeight
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
2018-12-20 16:25:22 +08:00
|
|
|
ctx.stroke();
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-11-08 00:06:32 +08:00
|
|
|
const fontSize = Math.round(badgeHeight * 0.5);
|
2018-12-20 16:25:22 +08:00
|
|
|
|
2020-08-31 18:14:09 +08:00
|
|
|
ctx.font = `${fontSize}px '${font}'`;
|
2016-11-08 00:06:32 +08:00
|
|
|
ctx.fillStyle = colors.primary;
|
2016-09-22 04:09:18 +08:00
|
|
|
ctx.fillText(
|
|
|
|
"all categories",
|
|
|
|
headerMargin * 1.5,
|
2018-12-20 16:25:22 +08:00
|
|
|
headerHeight + headerMargin * 1.4 + fontSize
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
|
|
|
|
2018-12-20 16:25:22 +08:00
|
|
|
const pathScale = badgeHeight / 1000;
|
|
|
|
// caret icon
|
|
|
|
const caretIcon = new Path2D(
|
|
|
|
"M0 384.662V127.338c0-17.818 21.543-26.741 34.142-14.142l128.662 128.662c7.81 7.81 7.81 20.474 0 28.284L34.142 398.804C21.543 411.404 0 402.48 0 384.662z"
|
|
|
|
);
|
|
|
|
|
|
|
|
ctx.save();
|
|
|
|
ctx.translate(
|
2016-09-22 04:09:18 +08:00
|
|
|
categoriesSize - headerMargin / 4,
|
2018-12-20 16:25:22 +08:00
|
|
|
headerHeight + headerMargin + badgeHeight / 4
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
2018-12-20 16:25:22 +08:00
|
|
|
ctx.scale(pathScale, pathScale);
|
|
|
|
ctx.fill(caretIcon);
|
|
|
|
ctx.restore();
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-11-08 00:06:32 +08:00
|
|
|
const text = opts.categories ? "Categories" : "Latest";
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-11-08 00:06:32 +08:00
|
|
|
const activeWidth = categoriesSize * (opts.categories ? 0.8 : 0.55);
|
2016-09-22 04:09:18 +08:00
|
|
|
ctx.beginPath();
|
|
|
|
ctx.fillStyle = colors.quaternary;
|
2018-06-15 23:03:24 +08:00
|
|
|
ctx.rect(
|
2016-09-22 05:34:15 +08:00
|
|
|
headerMargin * 2 + categoriesSize,
|
2016-11-08 00:06:32 +08:00
|
|
|
headerHeight + headerMargin,
|
|
|
|
activeWidth,
|
2016-09-22 05:34:15 +08:00
|
|
|
badgeHeight
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
2016-09-22 04:09:18 +08:00
|
|
|
ctx.fill();
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-08-31 18:14:09 +08:00
|
|
|
ctx.font = `${fontSize}px '${font}'`;
|
2016-09-22 04:09:18 +08:00
|
|
|
ctx.fillStyle = colors.secondary;
|
2016-11-08 00:06:32 +08:00
|
|
|
let x = headerMargin * 3.0 + categoriesSize;
|
2016-09-22 04:09:18 +08:00
|
|
|
ctx.fillText(
|
2018-06-15 23:03:24 +08:00
|
|
|
text,
|
2016-09-22 05:34:15 +08:00
|
|
|
x - headerMargin * 0.1,
|
2016-09-22 04:09:18 +08:00
|
|
|
headerHeight + headerMargin * 1.5 + fontSize
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
|
|
|
|
2016-09-22 04:09:18 +08:00
|
|
|
ctx.fillStyle = colors.primary;
|
2016-09-22 05:34:15 +08:00
|
|
|
x += categoriesSize * (opts.categories ? 0.8 : 0.6);
|
2016-09-22 04:09:18 +08:00
|
|
|
ctx.fillText("New", x, headerHeight + headerMargin * 1.5 + fontSize);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-09-22 04:09:18 +08:00
|
|
|
x += categoriesSize * 0.4;
|
|
|
|
ctx.fillText("Unread", x, headerHeight + headerMargin * 1.5 + fontSize);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2016-09-22 04:09:18 +08:00
|
|
|
x += categoriesSize * 0.6;
|
|
|
|
ctx.fillText("Top", x, headerHeight + headerMargin * 1.5 + fontSize);
|
2016-11-08 00:06:32 +08:00
|
|
|
},
|
2016-09-22 04:09:18 +08:00
|
|
|
},
|
2016-09-22 05:34:15 +08:00
|
|
|
obj
|
|
|
|
);
|
2016-09-17 04:12:56 +08:00
|
|
|
}
|
|
|
|
|
2016-09-22 04:09:18 +08:00
|
|
|
function loadImage(src) {
|
2016-09-23 02:39:36 +08:00
|
|
|
if (!src) {
|
2019-11-09 02:28:11 +08:00
|
|
|
return Promise.resolve();
|
2016-09-23 02:39:36 +08:00
|
|
|
}
|
|
|
|
|
2016-09-17 04:12:56 +08:00
|
|
|
const img = new Image();
|
2016-09-23 02:39:36 +08:00
|
|
|
img.src = getUrl(src);
|
2019-11-09 02:28:11 +08:00
|
|
|
return new Promise((resolve) => (img.onload = () => resolve(img)));
|
2016-09-17 04:12:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function parseColor(color) {
|
|
|
|
const m = color.match(/^#([0-9a-f]{6})$/i);
|
|
|
|
if (m) {
|
|
|
|
const c = m[1];
|
|
|
|
return [
|
2022-04-01 23:35:17 +08:00
|
|
|
parseInt(c.slice(0, 2), 16),
|
|
|
|
parseInt(c.slice(2, 4), 16),
|
|
|
|
parseInt(c.slice(4, 6), 16),
|
2016-09-17 04:12:56 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [0, 0, 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function brightness(color) {
|
|
|
|
return color[0] * 0.299 + color[1] * 0.587 + color[2] * 0.114;
|
|
|
|
}
|
|
|
|
|
|
|
|
function rgbToHsl(r, g, b) {
|
|
|
|
r /= 255;
|
|
|
|
g /= 255;
|
|
|
|
b /= 255;
|
|
|
|
let max = Math.max(r, g, b),
|
|
|
|
min = Math.min(r, g, b);
|
|
|
|
let h,
|
|
|
|
s,
|
|
|
|
l = (max + min) / 2;
|
|
|
|
|
|
|
|
if (max === min) {
|
|
|
|
h = s = 0;
|
|
|
|
} else {
|
|
|
|
const d = max - min;
|
|
|
|
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
|
|
switch (max) {
|
|
|
|
case r:
|
|
|
|
h = (g - b) / d + (g < b ? 6 : 0);
|
|
|
|
break;
|
|
|
|
case g:
|
|
|
|
h = (b - r) / d + 2;
|
|
|
|
break;
|
|
|
|
case b:
|
|
|
|
h = (r - g) / d + 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
h /= 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [h, s, l];
|
|
|
|
}
|
|
|
|
|
|
|
|
function hue2rgb(p, q, t) {
|
|
|
|
if (t < 0) {
|
|
|
|
t += 1;
|
|
|
|
}
|
|
|
|
if (t > 1) {
|
|
|
|
t -= 1;
|
|
|
|
}
|
|
|
|
if (t < 1 / 6) {
|
|
|
|
return p + (q - p) * 6 * t;
|
|
|
|
}
|
|
|
|
if (t < 1 / 2) {
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
if (t < 2 / 3) {
|
|
|
|
return p + (q - p) * (2 / 3 - t) * 6;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
function hslToRgb(h, s, l) {
|
|
|
|
let r, g, b;
|
|
|
|
|
|
|
|
if (s === 0) {
|
|
|
|
r = g = b = l; // achromatic
|
|
|
|
} else {
|
|
|
|
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
|
|
const p = 2 * l - q;
|
|
|
|
r = hue2rgb(p, q, h + 1 / 3);
|
|
|
|
g = hue2rgb(p, q, h);
|
|
|
|
b = hue2rgb(p, q, h - 1 / 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [r * 255, g * 255, b * 255];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function lighten(color, percent) {
|
|
|
|
const hsl = rgbToHsl(color[0], color[1], color[2]);
|
|
|
|
const scale = percent / 100.0;
|
|
|
|
const diff = scale > 0 ? 1.0 - hsl[2] : hsl[2];
|
|
|
|
|
|
|
|
hsl[2] = hsl[2] + diff * scale;
|
|
|
|
color = hslToRgb(hsl[0], hsl[1], hsl[2]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
"#" +
|
2022-04-01 23:35:17 +08:00
|
|
|
(0 | ((1 << 8) + color[0])).toString(16).slice(1) +
|
|
|
|
(0 | ((1 << 8) + color[1])).toString(16).slice(1) +
|
|
|
|
(0 | ((1 << 8) + color[2])).toString(16).slice(1)
|
2016-09-17 04:12:56 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function chooseBrighter(primary, secondary) {
|
|
|
|
const primaryCol = parseColor(primary);
|
|
|
|
const secondaryCol = parseColor(secondary);
|
|
|
|
return brightness(primaryCol) < brightness(secondaryCol)
|
|
|
|
? secondary
|
|
|
|
: primary;
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:00:20 +08:00
|
|
|
export function chooseDarker(primary, secondary) {
|
|
|
|
if (chooseBrighter(primary, secondary) === primary) {
|
|
|
|
return secondary;
|
|
|
|
} else {
|
|
|
|
return primary;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-17 04:12:56 +08:00
|
|
|
export function darkLightDiff(adjusted, comparison, lightness, darkness) {
|
|
|
|
const adjustedCol = parseColor(adjusted);
|
|
|
|
const comparisonCol = parseColor(comparison);
|
|
|
|
return lighten(
|
|
|
|
adjustedCol,
|
|
|
|
brightness(adjustedCol) < brightness(comparisonCol) ? lightness : darkness
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-22 04:09:18 +08:00
|
|
|
export function drawHeader(ctx, colors, width, headerHeight) {
|
2016-09-17 04:12:56 +08:00
|
|
|
ctx.save();
|
|
|
|
ctx.beginPath();
|
2016-09-22 04:09:18 +08:00
|
|
|
ctx.rect(0, 0, width, headerHeight);
|
2016-09-17 04:12:56 +08:00
|
|
|
ctx.fillStyle = colors.header_background;
|
|
|
|
ctx.shadowColor = "rgba(0, 0, 0, 0.25)";
|
|
|
|
ctx.shadowBlur = 2;
|
|
|
|
ctx.shadowOffsetX = 0;
|
|
|
|
ctx.shadowOffsetY = 2;
|
|
|
|
ctx.fill();
|
|
|
|
ctx.restore();
|
|
|
|
}
|