import time
import random
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
 
r = GPIO.PWM(22, 60)
g = GPIO.PWM(23, 60)
b = GPIO.PWM(24, 60)
r.start(0)
g.start(0)
b.start(0)
dcr = 0
dcg = 0
dcb = 0
try:
    while 1:
        #hexa = int(input("Farbe:"),16)
        #dcr = int(((hexa%(256*256*256))/(255*256*256))*100)
        #dcg = int(((hexa%(256*256))/(255*256))*100)
        #dcb = int(((hexa%256)/255)*100)
        #print(dcr,":",dcg,":",dcb)
        if dcr > 0:
            dcr = dcr + random.randint(-5,5)
        if dcr <= 0:
            dcr = dcr + random.randint(5,10)
        if dcr > 100:
            dcr = dcr + random.randint(-10,0)

        if dcg > 0:
            dcg = dcg + random.randint(-5,5)
        if dcg <= 0:
            dcg = dcg + random.randint(5,10)
        if dcr > 100:
            dcg = dcg + random.randint(-10,0)

        if dcb > 0:
            dcb = dcb + random.randint(-5,5)
        if dcb <= 0:
            dcb = dcb + random.randint(5,10)
        if dcb > 100:
            dcb = dcb + random.randint(-10,0)

        r.ChangeDutyCycle(dcr%101)
        g.ChangeDutyCycle(dcg%101)
        b.ChangeDutyCycle(dcb%101)
        hexr = int((dcr%101 * 255) / 100);
        hexg = int((dcg%101 * 255) / 100);
        hexb = int((dcb%101 * 255) / 100);
        hexa = hexb + hexg * 256 + hexr * 256 * 256
        print(f"{hexa:#08X}")
        time.sleep(0.01)
except KeyboardInterrupt:
    pass
    r.stop()
    g.stop()
    b.stop()
    GPIO.cleanup()
