Skip to content

Instantly share code, notes, and snippets.

@mdenson-dayspring
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdenson-dayspring/d13fecfd516a269f3fe8 to your computer and use it in GitHub Desktop.
Save mdenson-dayspring/d13fecfd516a269f3fe8 to your computer and use it in GitHub Desktop.
A Color Clock - Inspired by Hack-A-Day article http://hackaday.com/2012/01/25/very-accurate-clock-cant-be-read-accurately/.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Inspired by Hack-A-Day article http://hackaday.com/2012/01/25/very-accurate-clock-cant-be-read-accurately/.
#
# Author: Matthew Denson <mdenson AT dayspring-tech DOT com>
# Originally written for Processing: Feb 2, 2012
# Rewritten in Python/Tk: Oct 24, 2014
#
# Copyright (c) 2014 Dayspring Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from tkinter import Tk, Canvas, Frame, BOTH, ALL
from datetime import datetime
from math import sin, pi
import colorsys
value_night = 0.4
value_day = 0.9
value_diff = (value_day - value_night) * 0.5
class ColorClock(Frame):
def __init__(self,parent):
Frame.__init__(self,parent)
self.parent = parent
self.lastMin = 0
self.initUI()
def initUI(self):
self.parent.title("Color Clock")
self.pack(fill=BOTH, expand=1)
self.canvas = Canvas(self)
self.paintFrame()
def paintFrame(self):
self.prepFrameData()
self.paint()
self.after(40, self.paintFrame)
def paint(self):
self.canvas.delete(ALL)
self.canvas.create_rectangle(0, 0, 100, 100,
outline=self.colorW,
fill=self.colorW)
self.canvas.create_rectangle(100, 0, 200, 100,
outline=self.colorD,
fill=self.colorD)
self.canvas.create_rectangle(200, 0, 300, 100,
outline=self.colorH,
fill=self.colorH)
self.canvas.create_rectangle(300, 0, 400, 100,
outline=self.colorM,
fill=self.colorM)
self.canvas.pack(fill=BOTH, expand=1)
def prepFrameData(self):
dt = datetime.now()
if dt.minute!=self.lastMin:
self.parent.title(dt.strftime("%A %I %M %p"))
self.lastMin = dt.minute
fracSec = dt.microsecond / 1000000
fracMin = (dt.second+fracSec) / 60
fracHr = (dt.minute+fracMin) / 60
fracDay =(dt.hour+fracHr) / 24
fracWk = (dt.weekday()+fracDay) / 7
value = value_night + value_diff * (sin(fracDay * 2 * pi) + 1.0)
self.colorW = getRGBString(fracWk, value)
self.colorD = getRGBString(fracDay, value)
self.colorH = getRGBString(fracHr, value)
self.colorM = getRGBString(fracMin, value)
def getRGBString(hue, value):
rgb = colorsys.hsv_to_rgb(hue, 1.0, value)
rgb = tuple([int(255*x) for x in rgb])
return '#{0[0]:02x}{0[1]:02x}{0[2]:02x}'.format(rgb)
def main():
root = Tk()
ex = ColorClock(root)
root.geometry("400x100+300+300")
root.mainloop()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment