你可以爲此做一個嵌套循環。
如果你通過像素循環並設置顏色。
最簡單的事情就是使用hsv,然後轉換爲rgb。
因此,外部循環設置v(值),因爲這將是每行更改。
內循環將是h(色調),行中的每個像素。
你可以做這樣的事情。
from kivy.app import App
from kivy.graphics.texture import Texture
from kivy.graphics import Rectangle
from kivy.uix.boxlayout import BoxLayout
from colorsys import hsv_to_rgb
class MyLayout(BoxLayout):
def __init__(self,**kwargs):
super(MyLayout,self).__init__(**kwargs)
w = 64
h = 64
texture = Texture.create(size=(w, h))
buf = []
for i in range(h):
for j in range(w):
color = hsv_to_rgb(j/float(w),1,i/float(h)) # colorsys takes (0,0,0) to (1,1,1)
pixel = [int(a*b) for a,b in zip(color,[255,255,255])] # since color goes from (0,0,0) to (1,1,1), we multiply by 255
buf.extend(pixel)
buf = b''.join(map(chr, buf))
texture.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')
with self.canvas:
Rectangle(texture=texture, pos=self.pos, size=(w, h))
class MyApp(App):
def build(self):
return MyLayout()
if __name__ == "__main__":
MyApp().run()
不錯的工作謝謝,我試圖做一個像Instagram應用程序登錄屏幕變化的動畫。 – Juggernaut
謝謝你,我很開心:)我還沒有看到Instagram的登錄信息,但我猜想它的光譜會隨着時間而改變。 – EL3PHANTEN
是的,我應該使用動畫還是應該定義一個時間表來做到這一點?或者我應該使用pyjenius來編寫java? http://stackoverflow.com/questions/36726598/how-background-changes-at-login-in-instagram-app – Juggernaut