This site runs best with JavaScript enabled.

1. 根据 hex 背景颜色计算前景文字颜色

// 计算出经验灰度值,然后反色作为文字颜色
const getTextColorFromHex = (bgColor) => {
if (/^#([0-9a-f-A-F]{3}|[0-9a-f-A-F]{6})$/.test(bgColor))
return bgColor
.replace(/^#/, "")
.replace(/^\w+$/, c =>
c.length == 3
? c
.split("")
.map(c => c.repeat(2))
.join("")
: c
)
.match(/\w{2}/g)
.map(hexV => parseInt(hexV, 16))
.reduce((t, v, i) => t + [0.299, 0.587, 0.114][i] * v, 0) /
255 >
0.5
? "#000"
: "#fff"
else
return '#fff'
}

2. 快速生成一个 SSL 证书用来调试

openssl req -x509 -nodes -new -sha256 -days 1024 -newkey rsa:2048 -keyout localhost.key -out localhost.pem -subj "/C=US/CN=Example-Root-CA"
openssl x509 -outform pem -in localhost.pem -out localhost.crt

3. canvas 圆角矩形、圆角图片、多行文字

const roundRect = (ctx, x, y, w, h, r) => {
var min_size = Math.min(w, h);
if (r > min_size / 2) r = min_size / 2;
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
}
const roundImage = (ctx, image, x, y, w, h, r) => {
ctx.save()
ctx.beginPath();
roundRect(ctx, x, y, w, h, r);
ctx.clip()
ctx.closePath();
ctx.drawImage(image, 0, 0, image.width, image.height, x, y, w, h);
ctx.restore()
}
const drawText = (ctx, text, x, y, maxWidth, lineHeight) => {
let arrText = text.split('');
let line = '';
for (let n = 0; n < arrText.length; n++) {
let testLine = line + arrText[n];
let metrics = ctx.measureText(testLine);
let testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
ctx.fillText(line, x, y);
line = arrText[n];
y += lineHeight;
} else {
line = testLine;
}
}
ctx.fillText(line, x, y);
return y + lineHeight
}

4. 直接启动 AS 的模拟器

@echo off
if "%1" == "h" goto begin
mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit
:begin
::
D:\android_sdk\emulator\emulator.exe -netdelay none -netspeed full -avd fake_Pixel_3_XL_29

5. 直接静默启动 scrcpy,同时指定某个设备

@echo off
if "%1" == "h" goto begin
mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit
:begin
::
"D:\Program Files\scrcpy-win64-v1.16\scrcpy.exe" -s a9e8d32c
🔝

🐞 关于

有时间就会分享一些技术文章、专业内容、经典问题、系列功能等。

{⛔ 未标注内容均为原创,请勿转载 ©️}