诅咒(Python)中2种颜色之间的淡入

时间:2018-08-06 18:17:44

标签: python rgb ncurses curses

我正在尝试在代码中传递给setupColor()函数的2个任意RGB值之间淡化Curses子窗口的背景颜色。

在某些情况下,代码的行为符合预期,并且可以在各种颜色之间令人满意地淡入淡出,但大多数情况下,其执行情况会很奇怪。

setupColor((0,0,0),(0,255,55))

这将淡化黑色和浅绿色之间的子窗口,并且效果很好。

但是,如果我尝试在黄色和紫色之间淡入淡出,就像这样:

setupColor((255,200,0),(200,0,200))

这将在前几个周期中在两种颜色之间循环,但是似乎失去了同步性,最终产生的颜色与传递给函数的颜色明显不同。

当我将原始代码基于Arduino LED Fade草图时,我进行了搜索,看是否有人试图用物理RGB LED做类似的事情,从而打开了该线程:c++ Fade between colors? (Arduino)

发布在线程上的解决方案似乎很适合我的需求,但是我对C ++或JavaScript的了解不深,无法跟踪,理解并将代码移植到Python。

是否可以调整代码以使颜色正确地淡入淡出,还是值得对其进行剪贴并从头开始?

import curses,time
from curses import wrapper

def setupColor((red,green,blue),(red1,green1,blue1)):

    global color1,color2                                   #initialise globals for use later on in the program.
    global incrFloatRed,incrFloatGreen,incrFloatBlue
    global minRed,minGreen,minBlue,maxRed,maxGreen,maxBlue

    stepNumber = 60.00                 # number of steps within each color transition.

    color1 = (red,green,blue)         # clone the input tuples for use later on...
    color2 = (red1,green1,blue1)


    differenceRed = red - red1          # subtract each channel of color1 from color2,
    differenceGreen = green - green1    # this will return either a positive or negative float.
    differenceBlue = blue - blue1


    incrFloatRed = differenceRed / stepNumber        # divide the difference between the 2 colors by the 
    incrFloatGreen = differenceGreen / stepNumber    # step rate to obtain the color increments.
    incrFloatBlue = differenceBlue / stepNumber

    if red > red1:                            # if the red channel value of the 1st color is greater than 
            incrFloatRed = -incrFloatRed      # that of the 2nd, invert the increment (to allow
            maxRed = red                      # color subtraction), then set the top end of the range as 
            minRed = red1                     # red 1st channel and the bottom as red 2nd channel.
    else:                                     # Else, perform the inverse operation.
            incrFloatRed = abs(incrFloatRed)
            maxRed = red1
            minRed = red

    if green > green1:
            incrFloatGreen = -incrFloatGreen
            maxGreen = green
            minGreen = green1
    elif green < green1:
            incrFloatGreen = abs(incrFloatGreen)        
            maxGreen = green1
            minGreen = green

    if blue > blue1:
            incrFloatBlue = -incrFloatBlue
            maxBlue = blue
            minBlue = blue1
    else:
            incrFloatBlue = abs(incrFloatBlue)
            maxBlue = blue1
            minBlue = blue



def main(stdscr):
    global incrFloatRed,incrFloatGreen,incrFloatBlue

    setupColor((0,0,0),(0,255,255))

    red = color1[0]                #red,green and blue are the variables used to control the fade.
    green = color1[1]              #To begin with, they is set to the colors contained in the first 
    blue = color1[2]               #tuple that is passed to setupColor()



    label = stdscr.subwin(10,50,1,4)  # create a subwindow, draw a box around it, then add the string
    label.box()                       # "Hello, World!" to the subwindow at position row 1, column 1.
    label.addstr(1,1,"Hello, World!")
    curses.curs_set(0)                # Disable cursor visibility


    while True:




            red = red + incrFloatRed           # color = colorValue + colorIncrement
            green = green + incrFloatGreen
            blue = blue + incrFloatBlue

            if red <= minRed or red >= maxRed:   # if color is less than the start of the color range or
                    incrFloatRed = -incrFloatRed # greater than end of the color range, invert the color increment

            if green <= minGreen or green >= maxGreen:
                    incrFloatGreen = -incrFloatGreen

            if blue <= minBlue or blue >= maxBlue:
                    incrFloatBlue = -incrFloatBlue
                                                      # curses.init_color takes a tuple of rgb values as it's argument,
            cursesRed = int(int(red) / 0.255)         # but uses funky 1000-point intensity values, instead of the usual
            cursesGreen = int(int(green) / 0.255)     # 255. e.g. rgb(1000,0,0) for full intensity red, instead of
            cursesBlue = int(int(blue) / 0.255)       #           rgb(255,0,0).
                                                      # To convert between them, divide the integer of the color value float 
                                                      # by 0.255, then obtain the integer of the resulting float. 



            if cursesRed >=1000:                      # Sometimes a color value is set to greater
                    cursesRed = 1000                  # than 1k or less than 0. When a negative value or a value greater 
            if cursesGreen >=1000:                    # than 1k is passed to curses.init_color(), it will return ERR and
                    cursesGreen = 999                 # Curses will crash. 
            if cursesBlue >=1000:                     
                    cursesBlue = 999

            if cursesRed <=0:
                    cursesRed = 0
            if cursesGreen <=0:
                    cursesGreen = 0
            if cursesBlue <=0:
                    cursesBlue = 0


            curses.init_color(1,cursesRed,cursesGreen,cursesBlue) # reassign Curses color (1) to the RGB1K color of the current step... 

            curses.init_pair(1,255,1)                             # then create a color pair with the dynamic value (1) as 
                                                                  # the BG color, and white (255) as the FG. 



            label.bkgd(curses.color_pair(1)) # set the background of the label subwindow to the current color pair.. 
            label.refresh()                  # then refresh, so that we can see the change.

            time.sleep(0.02)                 # Take a little snooze, then do it all again. 


wrapper(main)

1 个答案:

答案 0 :(得分:0)

最后我自己弄清楚了这个问题,这是一个相对简单(但又笨拙又不雅致)的修复程序。我在程序的这一点上打印了每个颜色通道的输出(上面代码中的变量红色,绿色和蓝色):

if red <= minRed or red >= maxRed:   
    outFile.write("Red : " + str(int(red)) + "|Green : " + str(int(green)) + "|Blue : " + str(int(blue)) + "\n")
    incrFloatRed = -incrFloatRed 

这是程序的前三个周期的输出:

Red : 199|Green : 3  |Blue : 196
Red : 255|Green : 193|Blue : 0

Red : 199|Green : 9  |Blue : 196
Red : 255|Green : 186|Blue : 0

Red : 199|Green : 16 |Blue : 196
Red : 255|Green : 179|Blue : 0

如您所见,与其他通道相比,绿色通道逐渐偏离同步,并且红色和蓝色的值与传递给setupColor()函数的值略有不同。

可以通过使用if语句直接设置颜色值来解决颜色值不正确的问题:

while True:
        red = red + incrFloatRed           # color = colorValue + colorIncrement
        green = green + incrFloatGreen
        blue = blue + incrFloatBlue

        ##### Add the if statements after this ####

        if red < minRed:
            red = minRed
        if red > maxRed:
            red = maxRed

        if blue < minBlue:
            blue = minBlue
        if blue > maxBlue:
            blue = maxBlue

        if green < minGreen:
            green = minGreen
        if green > maxGreen:
            green = maxGreen

通过使用单个if语句更改颜色淡入方向,可以解决定时/同步问题。而不是像这样对每种颜色使用声明...

        if green <= minGreen or green >= maxGreen:
            incrFloatGreen = -incrFloatGreen 

...像这样一次设置所有它们:

        if green <= minGreen or green >=maxGreen:
            incrFloatRed = -incrFloatRed 
            incrFloatGreen = -incrFloatGreen
            incrFloatBlue = -incrFloatBlue   

鹰眼可能已经发现,如果将maxGreen和minGreen都设置为0(例如rgb(230,0,100)和rgb(100,0,200)),则不会发生任何事情。如果将绿色更改为其他颜色,它将可以正常工作。

我怀疑很难添加一些逻辑来检测哪些颜色通道将起作用,但是考虑到可以简单地通过传递1而不是0来避免这种情况,所以我没有感到烦恼。

另一个不错的做法(从效率角度来看)是将所有淡入淡出值转储到数组中,然后根据该值设置颜色,而不是每次都要花费计算值的开销。

相关问题