AIGameAIGame
首页
API文档
UI框架
下载软件
首页
API文档
UI框架
下载软件
  • API文档

    • $global - 全局函数
    • $act - 手势动作
      • 01.手势动作 - $act
      • 02.节点选择器 - UiSelector
      • 03.节点 - Node
    • $ag - 图色框架
    • $app - 应用操作
    • $arc - 悬浮菜单按钮
      • 01.悬浮菜单 - $arc
      • 02.悬浮容器 - MenuBody
      • 03.菜单按钮 - MenuItem
    • $bus - 消息总线
    • $color - 颜色操作
    • $crypt - 加密算法
    • $date - 日期工具
    • $device - 设备信息
    • $ext - dex,jar,so文件加载
    • $draw - 全局绘制
    • $engine - 脚本引擎
      • 01.任务信息 - JsTaskInfo
      • 02.脚本对象 - $task
    • $fc - 文件选择器
    • $file - 文件操作
    • $floaty - 悬浮窗
      • 01.可调节悬浮窗 - AdjFloaty
      • 02.系统级悬浮窗 - SysFloaty
      • 03.应用级悬浮窗 - AppFloaty
    • $img - 图片操作
    • $log - 日志框架
    • $ocr - 文字识别
    • $permit - 权限工具
    • $qr - 二维码工具
    • $res - 资源管理器
    • $root - ROOT与Shell命令
    • $screen - 屏幕操作
    • $storage - 应用内存储
    • $str - 字符串工具类
    • $sys - 系统操作
    • $thread - 并发编程
    • $tip - 对话框
    • $tts - 文字阅读
    • $yolo - 目标检测
    • $yolox - 目标检测

$color

  • 更新时间:2025-08-02 14:27:38

颜色操作

在$color中内置了很多常用的颜色,并且也有依据当前app主题动态获取对应颜色值的函数,尤其是在ui开发的过程中使用较多。

$color主要用来解析颜色,对比颜色,获取亮色暗色,为颜色设置透明度等等功能。

const {int} BLACK;

黑色

const {int} DGRAY;

深灰色

const {int} GRAY;

灰色

const {int} LGRAY;

浅灰色

const {int} WHITE;

白色

const {int} RED;

红色

const {int} GREEN;

绿色

const {int} BLUE;

蓝色

const {int} YELLOW;

黄色

const {int} CYAN;

青色

const {int} MAGENTA;

品红

const {int} TRANSPARENT;

透明

find(img, color, threshold, region)

查找颜色的位置

  • 参数 : img {Image} 在指定图片中找色
  • 参数 : color {String} 颜色值
  • 参数 : threshold {int} 阈值
  • 参数 : region {double[]} 范围
  • 返回 : {Point} 找到颜色的位置
  • 版本 : 1.0.0
//(1)截屏
let img = $screen.getScreen();
//(2)在屏幕上找到颜色
let point = $color.find(img, "#a55978", 5, [300,200,500,500]);
if (point != null) {
    //绘制出颜色的位置
    $draw.cross(point);
    sleep(3000);
}
img.close(); //回收截屏图片
$draw.closeAll(); //关闭绘制

find(img, color, threshold)

查找颜色的位置

  • 参数 : img {Image} 在指定图片中找色
  • 参数 : color {String} 颜色值
  • 参数 : threshold {int} 阈值
  • 返回 : {Point} 找到颜色的位置
  • 版本 : 1.0.0
//(1)截屏
let img = $screen.getScreen();
//(2)在屏幕上找到颜色
let point = $color.find(img, "#a55978", 5);
if (point != null) {
    //绘制出颜色的位置
    $draw.cross(point);
    sleep(3000);
}
img.close(); //回收截屏图片
$draw.closeAll(); //关闭绘制

find(img, color)

查找颜色的位置

  • 参数 : img {Image} 在指定图片中找色
  • 参数 : color {String} 颜色值
  • 返回 : {Point} 找到颜色的位置
  • 版本 : 1.0.0
//(1)截屏
let img = $screen.getScreen();
//(2)在屏幕上找到颜色
let point = $color.find(img, "#a55978");
if (point != null) {
    //绘制出颜色的位置
    $draw.cross(point);
    sleep(3000);
}
img.close(); //回收截屏图片
$draw.closeAll(); //关闭绘制

findAll(img, color, threshold, region)

查找颜色所有的位置

  • 参数 : img {Image} 在指定图片中找色
  • 参数 : color {String} 颜色值
  • 参数 : threshold {int} 阈值
  • 参数 : region {double[]} 范围
  • 返回 : {Point[]} 找到颜色的位置
  • 版本 : 1.0.0
let screenImg = $screen.getScreen();
let result = $color.findAll(screenImg,"#d28384",5,[]);
if(result!=null){
    for(let index of result){
        $draw.dot(index);//绘制位置的点
        $draw.log(index);//屏幕显示位置
        sleep(300);
    }
    //3秒后关闭悬浮绘制
    sleep(5000);
    $draw.closeAll();
}

findAll(img, color, threshold)

查找颜色所有的位置

  • 参数 : img {} 在指定图片中找色
  • 参数 : color {String} 颜色值
  • 参数 : threshold {int} 阈值
  • 返回 : {Point[]} 找到颜色的位置
  • 版本 : 1.0.0
let screenImg = $screen.getScreen();
let result = $color.findAll(screenImg,"#d28384",5);
if(result!=null){
    for(let index of result){
        $draw.dot(index);//绘制位置的点
        $draw.log(index);//屏幕显示位置
        sleep(300);
    }
    //3秒后关闭悬浮绘制
    sleep(5000);
    $draw.closeAll();
}

findAll(img, color)

查找颜色所有的位置

  • 参数 : img {} 在指定图片中找色
  • 参数 : color {String} 颜色值
  • 返回 : {Point[]} 找到颜色的位置
  • 版本 : 1.0.0
let screenImg = $screen.getScreen();
let result = $color.findAll(screenImg,"#d28384");
if(result!=null){
    for(let index of result){
        $draw.dot(index);//绘制位置的点
        $draw.log(index);//屏幕显示位置
        sleep(300);
    }
    //3秒后关闭悬浮绘制
    sleep(5000);
    $draw.closeAll();
}

similar(c1, c2)

计算相似度

  • 参数 : c1 {string|int} 颜色1
  • 参数 : c2 {string|int} 颜色2
  • 返回 : {double} 相似度
  • 版本 : 1.0.0
//计算相似度
let result = $color.similar("#1E1F22","#2B2D30");
log("相似度",result);

similar(c1, c2)

计算相似度

  • 参数 : c1 {string|int} 颜色1
  • 参数 : c2 {string|int} 颜色2
  • 返回 : {double} 相似度
  • 版本 : 1.0.0
//计算相似度
let result = $color.similar("#1E1F22","#2B2D30");
log("相似度",result);

similar(c1, c2)

计算相似度

  • 参数 : c1 {string|int} 颜色1
  • 参数 : c2 {string|int} 颜色2
  • 返回 : {double} 相似度
  • 版本 : 1.0.0
//计算相似度
let result = $color.similar("#1E1F22","#2B2D30");
log("相似度",result);

similar(c1, c2)

计算相似度

  • 参数 : c1 {string|int} 颜色1
  • 参数 : c2 {string|int} 颜色2
  • 返回 : {double} 相似度
  • 版本 : 1.0.0
//计算相似度
let result = $color.similar("#1E1F22","#2B2D30");
log("相似度",result);

toString(color)

颜色值转换为8位字符

  • 参数 : color {int} 颜色
  • 返回 : {string} 字符串颜色
  • 版本 : 1.0.0
//转为#00000000
let color = $color.toString(-166780);

str(color)

颜色值转6位字符

  • 参数 : color {int} 颜色值
  • 返回 : {string} 颜色
  • 版本 : 1.0.0
//转为#000000
let color = $color.str(-166780);

parse(r, g, b)

解析rgb的颜色值

  • 参数 : r {int} 红
  • 参数 : g {int} 绿
  • 参数 : b {int} 蓝
  • 返回 : {int} 颜色值
  • 版本 : 1.0.0
let color = $color.parse(105,78,230);

parse(color)

解析颜色值

该函数内置了M3风格主题的动态主题颜色,可以通过传入对应的主题字符串来获取对应的颜色值

  • 参数 : color {string} 颜色
  • 返回 : {int} 颜色值
  • 版本 : 1.0.0
//解析颜色值
let colorValue = $color.parse("#1E1F22");
log("颜色值",colorValue);
//除了上面的常规使用方法你也可以用$color动态获取主题颜色
//获取颜色值(返回int类型的颜色值)
let color = $color.parse("white");
let color = $color.parse("black");
let color = $color.parse("null");
let color = $color.parse("none");
let color = $color.parse("green");
let color = $color.parse("red");
let color = $color.parse("blue");
let color = $color.parse("yellow");
//主题颜色值(返回int类型的颜色值)
let color = $color.parse("colorPrimary");//(常用)一般是主题最明显的颜色:蓝色主题一般呈现蓝色
let color = $color.parse("colorSurface");//(常用)一般是背景颜色:亮色主题一般呈现白色,暗色主题一般呈现黑色
let color = $color.parse("colorOnSurface");//(常用)一般是文字颜色:亮色主题一般呈现黑色,暗色主题一般呈现白色
let color = $color.parse("colorOnPrimary");
let color = $color.parse("colorPrimaryContainer");
let color = $color.parse("colorOnPrimaryContainer");
let color = $color.parse("colorSecondary");
let color = $color.parse("colorOnSecondary");
let color = $color.parse("colorSecondaryContainer");
let color = $color.parse("colorOnSecondaryContainer");
let color = $color.parse("colorTertiary");
let color = $color.parse("colorOnTertiary");
let color = $color.parse("colorTertiaryContainer");
let color = $color.parse("colorOnTertiaryContainer");
let color = $color.parse("colorError");
let color = $color.parse("colorErrorContainer");
let color = $color.parse("colorOnErrorContainer");
let color = $color.parse("colorOnBackground");
let color = $color.parse("colorSurfaceVariant");
let color = $color.parse("colorOnSurfaceVariant");
let color = $color.parse("colorOutline");
let color = $color.parse("colorOutlineVariant");
let color = $color.parse("colorPrimaryInverse");

setAlpha(color, alpha)

设置颜色的透明度

  • 参数 : color {int} 颜色值
  • 参数 : alpha {int} 透明度(0-255)
  • 返回 : {int} 颜色值
  • 版本 : 1.3.0
//解析颜色值
let colorValue = $color.parse("#1E1F22");
//设置颜色的透明度
let color = $color.setAlpha(colorValue,100);

brighter(color)

获取较亮颜色

  • 参数 : color {int} 颜色值
  • 返回 : {int} 颜色值
  • 版本 : 1.3.0
//解析颜色值
let colorValue = $color.parse("#1E1F22");
//获取较亮颜色
let color = $color.brighter(colorValue);

darker(color)

获取较暗颜色值

  • 参数 : color {int} 颜色值
  • 返回 : {int} 颜色值
  • 版本 : 1.3.0
//解析颜色值
let colorValue = $color.parse("#1E1F22");
//获取较暗颜色值
let color = $color.darker(colorValue);

rgb(red, green, blue)

通过RGB通道获得颜色值

  • 参数 : red {int} R通道
  • 参数 : green {int} G通道
  • 参数 : blue {int} B通道
  • 返回 : {int} 颜色值
  • 版本 : 1.0.0
let color = $color.rgb(105,78,230);

argb(alpha, red, green, blue)

通过ARGB通道获得颜色值

  • 参数 : alpha {int} A通道
  • 参数 : red {int} R通道
  • 参数 : green {int} G通道
  • 参数 : blue {int} B通道
  • 返回 : {int} 颜色值
  • 版本 : 1.0.0
let color = $color.argb(255,105,78,230);

a(color)

计算[A通道]的值

  • 参数 : color {String} 颜色
  • 返回 : {int} 数值
  • 版本 : 1.0.0
let a = $color.a("#357C94");

r(color)

计算[R通道]的值

  • 参数 : color {String} 颜色
  • 返回 : {int} 数值
  • 版本 : 1.0.0
let r = $color.r("#357C94");

g(color)

计算[G通道]的值

  • 参数 : color {String} 颜色
  • 返回 : {int} 数值
  • 版本 : 1.0.0
let g = $color.g("#357C94");

b(color)

计算[B通道]的值

  • 参数 : color {String} 颜色
  • 返回 : {int} 数值
  • 版本 : 1.0.0
let b = $color.b("#357C94");

equals(c1, c2)

比较两个颜色

  • 参数 : c1 {string|int} 颜色1
  • 参数 : c2 {string|int} 颜色2
  • 返回 : {boolean} 是否相等
  • 版本 : 1.0.0
let color = "#1E1F22";
let colorValue =-14803166;
let same = $color.equals(color,colorValue);
if(same){
    toast("颜色相同");
}else{
    toast("颜色不同");
}

equals(c1, c2)

比较两个颜色

  • 参数 : c1 {string|int} 颜色1
  • 参数 : c2 {string|int} 颜色2
  • 返回 : {boolean} 是否相等
  • 版本 : 1.0.0
let color = "#1E1F22";
let colorValue =-14803166;
let same = $color.equals(color,colorValue);
if(same){
    toast("颜色相同");
}else{
    toast("颜色不同");
}

equals(c1, c2)

比较两个颜色

  • 参数 : c1 {string|int} 颜色1
  • 参数 : c2 {string|int} 颜色2
  • 返回 : {boolean} 是否相等
  • 版本 : 1.0.0
let color = "#1E1F22";
let colorValue =-14803166;
let same = $color.equals(color,colorValue);
if(same){
    toast("颜色相同");
}else{
    toast("颜色不同");
}

equals(c1, c2)

比较两个颜色

  • 参数 : c1 {string|int} 颜色1
  • 参数 : c2 {string|int} 颜色2
  • 返回 : {boolean} 是否相等
  • 版本 : 1.0.0
let color = "#1E1F22";
let colorValue =-14803166;
let same = $color.equals(color,colorValue);
if(same){
    toast("颜色相同");
}else{
    toast("颜色不同");
}

isDarkTheme()

是否是黑暗主题

  • 返回 : {boolean} 是否是深色主题
let isDark = $color.isDarkTheme();
if(isDark){
    toast("黑暗主题");
}else{
    toast("亮色主题");
}

isColor(colorStr)

判断颜色格式是否正确

  • 参数 : colorStr {string} 颜色字符串
  • 返回 : {boolean} 是否是正确的颜色格式
  • 版本 : 1.4.3
let isColor = $color.isColor("#1E1F22");
if(isColor){
    toast("颜色格式正确");
}else{
    toast("颜色格式错误");
}
最近更新: 2025/5/14 08:43
Contributors: 孑小白
Prev
$bus - 消息总线
Next
$crypt - 加密算法