诅咒中两种颜色之间的淡出(Python)

2024-10-02 18:28:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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

在某些情况下,代码的行为与预期的一样,并且可以令人满意地在颜色之间淡出,但大多数时候它的执行很奇怪。在

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

这将使子窗口在黑色和浅绿色之间褪色,效果很好。在

但是,如果我试着在黄色和紫色之间褪色,就像这样:

^{pr2}$

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

当我把原始代码建立在Arduino LED淡出草图的基础上时,我搜索是否有人尝试过使用物理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)

Tags: orofthetoifgreenbluered
1条回答
网友
1楼 · 发布于 2024-10-02 18:28:28

最后我自己想出来了,这是一个相对简单(但又粗糙又不雅观)的修复。我在程序中打印了每个颜色通道(上面代码中的变量红色、绿色和蓝色)的输出:

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

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

^{pr2}$

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

通过使用if语句直接设置值,可以修复颜色值的不精确性:

^{3}$

定时/同步问题可以通过使用单个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,0100)和rgb(100,0200)),则不会发生任何事情。如果你把绿色换成不同的颜色,就可以了。在

我怀疑很难添加一些逻辑来检测哪些颜色通道可以工作,但是考虑到只需传递1而不是0就可以避免这一点,我并不介意。在

另一个好的方法(效率方面)是将所有的淡入度值转储到一个数组中,并基于该数组设置颜色,而不是每次都要花费计算值的开销。在

相关问题 更多 >