def initColoursForSensor(sensor): Color.GREENBALL = Color(h=133, s=96, v=37) Color.REDBALL = Color(h=3, s=98, v=22) Color.BLUEBALL = Color(h=216, s=97, v=53) Color.YELLOWBALL = Color(h=48, s=98, v=17) Color.ORANGEBALL = Color(h=19, s=96, v=32) Color.PURPLEBALL = Color(h=286, s=77, v=29) sensor.detectable_colors( (Color.WHITE, Color.NONE, Color.GREENBALL, Color.REDBALL, Color.BLUEBALL, Color.YELLOWBALL, Color.ORANGEBALL, Color.PURPLEBALL))
from pybricks.pupdevices import ColorDistanceSensor from pybricks.parameters import Port, Color from pybricks.tools import wait # Initialize the sensor. sensor = ColorDistanceSensor(Port.A) # First, decide which objects you want to detect, and measure their HSV values. # You can do that with the hsv() method as shown in the previous example. # # Use your measurements to override the default colors, or add new colors: Color.GREEN = Color(h=132, s=94, v=26) Color.MAGENTA = Color(h=348, s=96, v=40) Color.BROWN = Color(h=17, s=78, v=15) Color.RED = Color(h=359, s=97, v=39) # Put your colors in a list or tuple. my_colors = (Color.GREEN, Color.MAGENTA, Color.BROWN, Color.RED, Color.NONE) # Save your colors. sensor.detectable_colors(my_colors) # color() works as usual, but now it returns one of your specified colors. while True: color = sensor.color() # Print the color. print(color) # Check which one it is. if color == Color.MAGENTA:
from pybricks.hubs import TechnicHub from pybricks.parameters import Color from pybricks.tools import wait from math import sin, pi # Initialize the hub. hub = TechnicHub() # Make an animation with multiple colors. hub.light.animate([Color.RED, Color.GREEN, Color.NONE], interval=500) wait(10000) # Make the color RED grow faint and bright using a sine pattern. hub.light.animate( [Color.RED * (0.5 * sin(i / 15 * pi) + 0.5) for i in range(30)], 40) wait(10000) # Cycle through a rainbow of colors. hub.light.animate([Color(h=i * 8) for i in range(45)], interval=40) wait(10000)
from pybricks.hubs import TechnicHub from pybricks.parameters import Color from pybricks.tools import wait # Initialize the hub. hub = TechnicHub() # Show the color at 30% brightness. hub.light.on(Color.RED * 0.3) wait(2000) # Use your own custom color. hub.light.on(Color(h=30, s=100, v=50)) wait(2000) # Go through all the colors. for hue in range(360): hub.light.on(Color(hue)) wait(10)
from pybricks.pupdevices import ColorSensor from pybricks.parameters import Port, Color from pybricks.tools import wait # Initialize the sensor. sensor = ColorSensor(Port.A) # First, decide which objects you want to detect. # Then measure their color with the hsv() method, # as shown in the previous example. Write them down # as shown below. The name is optional, but it is # useful when you print the color value. green = Color(h=132, s=94, v=26, name='GREEN_BRICK') magenta = Color(h=348, s=96, v=40, name='MAGENTA_BRICK') brown = Color(h=17, s=78, v=15, name='BROWN_BRICK') red = Color(h=359, s=97, v=39, name='RED_BRICK') # Put your colors in a list or tuple. # Including None is optional. Just omit it if # you always want to get one of your colors. my_colors = (green, magenta, brown, red, None) # Save your colors. sensor.detectable_colors(my_colors) # color() works as usual, but it only # returns one of your specified colors. while True: color = sensor.color() # Print the color.
If you give no arguments, the currently chosen colors will be returned as a tuple. Parameters: colors (list) – List of Color objects: the colors that you want to detect. You can pick standard colors such as Color.MAGENTA, or provide your own colors like Color(h=348, s=96, v=40, name='MY_MAGENTA_BRICK') for even better results. You measure your own colors with the hsv() method. """ # correct Color values green = Color(h=132, s=94, v=26) magenta = Color(h=348, s=96, v=40) brown = Color(h=17, s=78, v=15) red = Color(h=359, s=97, v=39) my_colors = (green, magenta, brown, red, Color.NONE) sensor.detectable_colors(my_colors) # Limits: Hue (0–359) Saturation (0–100) brightness Value (0–100) # at the moment there is **no** validation of the values given here w1_max = Color(h=359, s=100, v=100) w2_max = Color(h=1000, s=1000, v=1100) w3Err = Color(h=36, s=-1, v=25) error_colors = (w1_max, w2_max, w3Err, Color.NONE) sensor.detectable_colors(error_colors)
If you give no arguments, the currently chosen colors will be returned as a tuple. Parameters: colors (list) – List of Color objects: the colors that you want to detect. You can pick standard colors such as Color.MAGENTA, or provide your own colors like Color(h=348, s=96, v=40, name='MY_MAGENTA_BRICK') for even better results. You measure your own colors with the hsv() method. """ # correct Color values green = Color(h=132, s=94, v=26, name='GREEN_BRICK') magenta = Color(h=348, s=96, v=40, name='MAGENTA_BRICK') brown = Color(h=17, s=78, v=15, name='BROWN_BRICK') red = Color(h=359, s=97, v=39, name='RED_BRICK') my_colors = (green, magenta, brown, red, None) sensor.detectable_colors(my_colors) # Limits: Hue (0–359) Saturation (0–100) brightness Value (0–100) # at the moment there is **no** validation of the values given here w1_max = Color(h=359, s=100, v=100) w2_max = Color(h=1000, s=1000, v=1100) w3Err = Color(h=36, s=-1, v=25) error_colors = (w1_max, w2_max, w3Err, None) sensor.detectable_colors(error_colors)
from pybricks.parameters import Color # You can print colors. Colors may be obtained from the Color class, or # from sensors that return color measurements. print(Color.RED) # You can read hue, saturation, and value properties. print(Color.RED.h, Color.RED.s, Color.RED.v) # You can make your own colors. Saturation and value are 100 by default. my_green = Color(h=125) my_dark_green = Color(h=125, s=80, v=30) # When you print custom colors, you see exactly how they were defined. print(my_dark_green) # You can also add colors to the builtin colors. Color.MY_DARK_BLUE = Color(h=235, s=80, v=30) # When you add them like this, printing them only shows its name. But you can # still read h, s, v by reading its attributes. print(Color.MY_DARK_BLUE) print(Color.MY_DARK_BLUE.h, Color.MY_DARK_BLUE.s, Color.MY_DARK_BLUE.v)
from pybricks.hubs import TechnicHub from pybricks.pupdevices import ColorDistanceSensor from pybricks.parameters import Port, Color from pybricks.tools import wait sensor = ColorDistanceSensor(Port.B) led = TechnicHub().light lastReading = None while True: wait(100) newReading = sensor.distance() if newReading != lastReading: print(newReading) lastReading = newReading if newReading == 100: led.off() else: led.on(Color(newReading * 3))