在图片上, 随机取25个点 把颜色转为hsl, 若是25个点中, 有70%的颜色都是蓝色, 那么图片的主题色就是蓝色java
// 蓝色240度
// 实测画面蓝色范围 210-240
// 70%的点都是蓝色, 说明没有弹框
importClass(Packages.androidx.core.graphics.ColorUtils);
function 主色是否是蓝色(img) {
let dw = device.width;
let dh = device.height;
let left = dw * 0.3;
let right = dw * 0.7;
let top = dh * 0.3;
let bottom = dh * 0.5;
let pointList = [];
let k = 5;
let unitX = (right - left) / k;
let unitY = (bottom - top) / k;
for (var i = 0; i < k; i++) {
let y = top + i * unitY;
for (var j = 0; j < k; j++) {
let x = left + j * unitX;
pointList.push({
x: x,
y: y,
});
}
}
let blueCount = 0;
let total = k * k;
pointList.map(function (point) {
let color = img.pixel(point.x, point.y);
let hsl = util.java.array("float", 3);
ColorUtils.RGBToHSL(colors.red(color), colors.green(color), colors.blue(color), hsl);
if (hsl[0] > 210 && hsl[0] < 240) {
blueCount++;
}
});
let limit = 0.7;
if (blueCount / total > limit) {
return true;
} else {
return false;
}
}
module.exports = 主色是否是蓝色;
复制代码
部份内容来自网络 本教程仅用于学习, 禁止用于其余用途android