vany5921
Published © MIT

M5ColorUnit-Colorimeter

Colorimeter\Color recognition\Color overlay

IntermediateFull instructions provided2 hours2
M5ColorUnit-Colorimeter

Things used in this project

Hardware components

ESP32 Basic Core IoT Development Kit
M5Stack ESP32 Basic Core IoT Development Kit
×1
M5Stack Color Unit
×1

Software apps and online services

UIFlow

Story

Read more

Code

Colormeter

Python
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

#Identify color 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)
#Identify color 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)
#Calculate color difference
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
  #addition
  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)
  #subtraction
  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)

Credits

auzn

Posted by vany5921

Comments

Add projectSign up / Login