最早遇到M5STACK是在深圳制汇节,一眼就喜欢上这了个可推叠的小盒子,前后core stick atom都买了(个_个), 这次很高兴可以试用Color Unit。
Color Unit
中间是TCS3472 两边各有一个LED辅助照明,可以通过I2C与core 通信,m5 Unit一般都可以与乐高机械连接。
连接非常方便,m5 core 可以多应用,爱折腾挺方便的
功能实现
- 颜色识别:按A、B键可识别两个颜色并展示色块及RGB数值
- 色差计算:按C键可计算两个颜色色差这里用的简单算法,参照 https://blog.csdn.net/qq_16564093/article/details/80698479
- 色彩叠加:用色光加色法、色料减色法 https://baike.baidu.com/item/%E4%B8%89%E5%8E%9F%E8%89%B2 计算,算法一样简陋~(-_-~)~
UI Flow
代码
from m5stack import *
from m5ui import *
from uiflow import *
import unit
setScreenColor(0x222222)
color0 = unit.get(unit.COLOR, unit.PORTA)
title = M5Title(title="color-difference meter", x=3 , fgcolor=0xFFFFFF, bgcolor=0x0000FF)
l1_raw = M5TextBox(121, 30, "_________", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelG1 = M5TextBox(79, 49, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelR1 = M5TextBox(42, 49, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelB1 = M5TextBox(115, 51, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
rectangle0 = M5Rect(4, 32, 30, 30, 0xFFFFFF, 0xFFFFFF)
label1 = M5TextBox(40, 30, "RawData1:", lcd.FONT_Default,0xFFFFFF, rotate=0)
rectangle1 = M5Rect(4, 80, 30, 30, 0xFFFFFF, 0xFFFFFF)
label0 = M5TextBox(36, 80, "RawData2:", lcd.FONT_Default,0xFFFFFF, rotate=0)
label3 = M5TextBox(5, 155, "Mix:", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelR2 = M5TextBox(40, 96, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelG2 = M5TextBox(77, 97, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
rectangle2 = M5Rect(72, 154, 30, 30, 0xFFFFFF, 0xFFFFFF)
labelR3 = M5TextBox(115, 171, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelB2 = M5TextBox(113, 99, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
l2_raw = M5TextBox(117, 78, "__________", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelG3 = M5TextBox(158, 169, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelB3 = M5TextBox(200, 169, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
label2 = M5TextBox(4, 127, "difference:", lcd.FONT_Default,0xFFFFFF, rotate=0)
rectangle3 = M5Rect(72, 199, 30, 30, 0xFFFFFF, 0xFFFFFF)
labelDiff = M5TextBox(85, 128, "___", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelR4 = M5TextBox(115, 213, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelG4 = M5TextBox(159, 212, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
labelB4 = M5TextBox(199, 212, "__", lcd.FONT_Default,0xFFFFFF, rotate=0)
label4 = M5TextBox(52, 162, "+", lcd.FONT_Default,0xFFFFFF, rotate=0)
label5 = M5TextBox(53, 207, "-", lcd.FONT_Default,0xFFFFFF, rotate=0)
import math
colorR2 = None
colorR1 = None
rmean = None
colorR3 = None
colorG2 = None
colorG1 = None
difference = None
colorG3 = None
colorB2 = None
colorB1 = None
colorB3 = None
colorR4 = None
colorG4 = None
colorB4 = None
#识别颜色1
def buttonB_wasPressed():
global colorR2, colorR1, rmean, colorR3, colorG2, colorG1, difference, colorG3, colorB2, colorB1, colorB3, colorR4, colorG4, colorB4
colorR2 = color0.red
colorG2 = color0.green
colorB2 = color0.blue
l2_raw.setText(str(color0.rawData))
labelR2.setText(str(colorR2))
labelG2.setText(str(colorG2))
labelB2.setText(str(colorB2))
rgb.setColorFrom(1 , 5 ,(colorR2 << 16) | (colorG2 << 8) | colorB2)
rectangle1.setBgColor((colorR2 << 16) | (colorG2 << 8) | colorB2)
pass
btnB.wasPressed(buttonB_wasPressed)
#识别颜色2
def buttonA_wasPressed():
global colorR2, colorR1, rmean, colorR3, colorG2, colorG1, difference, colorG3, colorB2, colorB1, colorB3, colorR4, colorG4, colorB4
colorR1 = color0.red
colorG1 = color0.green
colorB1 = color0.blue
l1_raw.setText(str(color0.rawData))
labelR1.setText(str(colorR1))
labelG1.setText(str(colorG1))
labelB1.setText(str(colorB1))
rgb.setColorFrom(6 , 10 ,(colorR1 << 16) | (colorG1 << 8) | colorB1)
rectangle0.setBgColor((colorR1 << 16) | (colorG1 << 8) | colorB1)
pass
btnA.wasPressed(buttonA_wasPressed)
#计算色差
def buttonC_wasPressed():
global colorR2, colorR1, rmean, colorR3, colorG2, colorG1, difference, colorG3, colorB2, colorB1, colorB3, colorR4, colorG4, colorB4
rmean = (colorR1 + colorR2) / 2
difference = math.sqrt((2 + rmean / 256) * (colorR1 - colorR2) ** 2 + (4 * (colorG1 - colorG2) ** 2) + (2 + (255 - rmean) / 256 * (colorB1 - colorB2) ** 2))
labelDiff.setText(str(difference))
pass
btnC.wasPressed(buttonC_wasPressed)
def buttonC_wasReleased():
global colorR2, colorR1, rmean, colorR3, colorG2, colorG1, difference, colorG3, colorB2, colorB1, colorB3, colorR4, colorG4, colorB4
#加法
colorR3 = min([colorR1 + colorR2, 255])
colorG3 = min([colorG1 + colorG2, 255])
colorB3 = min([colorB1 + colorB2, 255])
labelR3.setText(str(colorR3))
labelG3.setText(str(colorG3))
labelB3.setText(str(colorB3))
rectangle2.setBgColor((colorR3 << 16) | (colorG3 << 8) | colorB3)
#减法
colorR4 = max([max([colorR2 - (256 - colorR1), colorR1 - (256 - colorR2)]), 0])
colorG4 = max([max([colorG2 - (256 - colorG1), colorG1 - (256 - colorG2)]), 0])
colorB4 = max([max([colorB2 - (256 - colorB1), colorB1 - (256 - colorB2)]), 0])
labelR4.setText(str(colorR4))
labelG4.setText(str(colorG4))
labelB4.setText(str(colorB4))
rectangle3.setBgColor((colorR4 << 16) | (colorG4 << 8) | colorB4)
pass
btnC.wasReleased(buttonC_wasReleased)
演示
色差
加法
减法
视频