[翻译] ColourClock 将时间值转换成背景色

简介:

ColourClock 将时间值转换成背景色

 

https://github.com/bennyguitar/ColourClock

This project converts Time to Hex/RGB, and is quite beautiful to look at. This was HEAVILY inspired byhttp://thecolourclock.co.uk and really, all credit goes to them.

这个工程是用来把时间值转换为Hex/RGB值的,看起来非常漂亮。灵感来自于这个网站 http://thecolourclock.co.uk

  

 

 

使用思路:

将一个要根据时间改变颜色View中layer的backgroundcolor赋值即可动态改变颜色。

 

附录:

ViewController.h


//
//  ViewController.h
//  ColourClock
//
//  Created by Ben Gordon on 12/20/12.
//  Copyright (c) 2012 Ben Gordon. All rights reserved.
//

#import <UIKit/UIKit.h>

enum ClockType {
    ClockTypeMilitary = 0,
    ClockTypeHex = 1,
    ClockTypeRGB = 2
};

@interface ViewController : UIViewController {
    
    __weak IBOutlet UILabel *timeLabel;
    __weak IBOutlet UILabel *appearanceType;
    
    enum ClockType currentType;
}


- (IBAction)changeClockType:(id)sender;

@end

ViewController.m


//
//  ViewController.m
//  ColourClock
//
//  Created by Ben Gordon on 12/20/12.
//  Copyright (c) 2012 Ben Gordon. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

#pragma mark - View Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    currentType = ClockTypeMilitary;
    [self changeColor];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Change Colors

-(void)changeColor {
    // Set up date formatters for hour, min, seconds.
    // Then create strings from the current date.
    NSDateFormatter *formatHour = [[NSDateFormatter alloc] init];
    NSDateFormatter *formatMin = [[NSDateFormatter alloc] init];
    NSDateFormatter *formatSec = [[NSDateFormatter alloc] init];
    [formatHour setDateFormat:@"HH"];
    [formatMin setDateFormat:@"mm"];
    [formatSec setDateFormat:@"ss"];
    NSString *hour = [formatHour stringFromDate:[NSDate date]];
    NSString *minute = [formatMin stringFromDate:[NSDate date]];
    NSString *second = [formatSec stringFromDate:[NSDate date]];
    
    
    // Create floats of the time value.
    float hourFloat = [hour floatValue] * 255.0f / 23.0f;
    float minFloat = [minute floatValue] * 255.0f / 59.0f;
    float secFloat = [second floatValue] * 255.0f / 59.0f;
    
    
    // Create unsigned ints for Hex translation
    int32_t hourint = hourFloat + 0.5;
    int32_t minint = minFloat + 0.5;
    int32_t secint = secFloat + 0.5;
    
    
    // Change text color so it's readable.
    if (hourFloat > 200 && minFloat > 200 && secFloat > 200) {
        timeLabel.textColor = [UIColor darkGrayColor];
        appearanceType.textColor = [UIColor darkGrayColor];
    }
    else {
        timeLabel.textColor = [UIColor whiteColor];
        appearanceType.textColor = [UIColor whiteColor];
    }
    
    
    // Set Labels
    if (currentType == ClockTypeMilitary) {
        appearanceType.text = @"MILITARY TIME";
        timeLabel.text = [NSString stringWithFormat:@"%@:%@:%@", hour, minute, second];
    }
    else if (currentType == ClockTypeHex) {
        appearanceType.text = @"HEX COLOR CODE";
        timeLabel.text = [NSString stringWithFormat:@"#%02X%02X%02X",hourint,minint,secint];
    }
    else {
        appearanceType.text = @"RGB VALUES";
        timeLabel.text = [NSString stringWithFormat:@"%.0f:%.0f:%.0f", hourFloat, minFloat, secFloat];
    }
    
    
    // Finally, change image to the right color
    self.view.backgroundColor = [UIColor colorWithRed:(hourFloat/255.0f) green:(minFloat/255.0f) blue:(secFloat/255.0f) alpha:1.0];
    
    // And do it all over again, every .05 seconds so it's more accurate
    [self performSelector:@selector(changeColor) withObject:nil afterDelay:0.05];
}




#pragma mark - Change Clock Type

- (IBAction)changeClockType:(id)sender {
    currentType++;
    
    if (currentType > ClockTypeRGB) {
        currentType = ClockTypeMilitary;
    }
}

@end


目录
相关文章
【MATLAB第11期】#源码分享 |时间序列数据绘图,横坐标更改为时间轴 横坐标轴参数更改 日期间隔设置 日期标签或格式更改
【MATLAB第11期】#源码分享 |时间序列数据绘图,横坐标更改为时间轴 横坐标轴参数更改 日期间隔设置 日期标签或格式更改
|
9月前
|
容器
文本溢出省略号text-overflow: ellipsis显示无效?这一属性到底该怎么用?
文本溢出省略号text-overflow: ellipsis显示无效?这一属性到底该怎么用?
|
6月前
|
JavaScript 前端开发 定位技术
JavaScript如何操作实现一个地图并标记到自身当前位置(本文从创建key值开始,到实现标点结束)
JavaScript如何操作实现一个地图并标记到自身当前位置(本文从创建key值开始,到实现标点结束)
44 0
JavaScript如何操作实现一个地图并标记到自身当前位置(本文从创建key值开始,到实现标点结束)
文本超出显示省略号--截取字段
文本超出显示省略号--截取字段
textarea文本域控制字数多少(带数字,数字减小)demo效果示例(整理)
textarea文本域控制字数多少(带数字,数字减小)demo效果示例(整理)
|
9月前
Echarts参数属性学习:x轴标签文本过长自动缩减并替换成缩略号...
Echarts参数属性学习:x轴标签文本过长自动缩减并替换成缩略号...
62 0
|
11月前
|
前端开发
前端学习案例2-文本溢出-呈现圆点显示2多行文本溢出
前端学习案例2-文本溢出-呈现圆点显示2多行文本溢出
52 0
HTML颜色码对照表-英文代码、中文描述、十六进制、rgb值
HTML颜色码对照表-英文代码、中文描述、十六进制、rgb值
166 0
HTML颜色码对照表-英文代码、中文描述、十六进制、rgb值
135.设置图形方式下的文本类型
135.设置图形方式下的文本类型
52 0
VB编程:利用数组统计文字段落数-31
VB编程:利用数组统计文字段落数-31