ColorUtilities 拓展:16进制颜色转换为AppInventor标准颜色

使用案例
根据主题色生成界面颜色
ColorUtilities 是不可见扩展。下面的积木把十六进制颜色转换为 App Inventor 颜色,再赋给布局和标签:
when Screen1.Initialize() {
标签_结果.TextColor = ColorUtilities1.ConvertHexToInt("#4CAF50")
垂直布局_内容.BackgroundColor = ColorUtilities1.ConvertHexToInt("#4CAF50")
}
还可以使用以下积木生成按钮的按下态颜色:
when 按钮_主题.Click() {
按钮_主题.BackgroundColor = ColorUtilities1.DarkenColor(
ColorUtilities1.ConvertHexToInt("#4CAF50"), 0.2)
}
还可以在同一事件中判断背景色明暗、选择文字颜色,并把整数颜色转换回十六进制文本:
when 按钮_检查颜色.Click() {
if ColorUtilities1.IsDarkColor(ColorUtilities1.ConvertHexToInt("#4CAF50")) {
标签_结果.TextColor = ColorUtilities1.LightenColor(
ColorUtilities1.ConvertHexToInt("#4CAF50"), 0.8)
} else {
标签_结果.TextColor = ColorUtilities1.DarkenColor(
ColorUtilities1.ConvertHexToInt("#4CAF50"), 0.2)
}
标签_颜色.Text = ColorUtilities1.ConvertIntToHex(
ColorUtilities1.ConvertHexToInt("#4CAF50"))
}
方法
- BlendColors混合颜色(颜色1,颜色2,比例)
- 把两个颜色按比例混合成一个新颜色。比例取 0 到 1 之间的值:0 全取颜色1,1 全取颜色2。
- ConvertHexToInt十六进制转整数(十六进制颜色)
- 把十六进制颜色(如 #FF0000)转换为 App Inventor 标准的整数颜色。
- ConvertIntToHex整数转十六进制(整数颜色)
- 把 App Inventor 的整数颜色转换为十六进制颜色文本。
- DarkenColor加深颜色(颜色,系数)
- 按给定系数把颜色加深。
- LightenColor变浅颜色(颜色,系数)
- 按给定系数把颜色变浅。
- GetLuminance获取亮度(颜色)
- 返回颜色的亮度值,为 0 到 1 之间的小数。
- IsDarkColor是否深色(颜色)
- 判断该颜色是深色还是浅色。
拓展下载
- .aix 拓展下载:
com.gordonlu.ColorUtilities.aix
版权信息
本文档内容依据原作者发布页面整理翻译,版权归原作者所有。
- 原作者:Gordon Lu
- 原始网址:https://community.appinventor.mit.edu/t/f-os-colorutilities-some-tools-to-work-with-colors/56116
扫码添加客服咨询