JavaScript 颜色梯度和渐变效果(4)
作者:cloudgamer 来源:cloudgamer博客 发布时间:2009-03-18 11:16:00
程序代码
ColorGrads部分:
var ColorGrads = function(options){
this.SetOptions(options);
this.StartColor = this.options.StartColor;
this.EndColor = this.options.EndColor;
this.Step = Math.abs(this.options.Step);
};
ColorGrads.prototype = {
//设置默认属性
SetOptions: function(options) {
this.options = {//默认值
StartColor: "#fff",//开始颜色
EndColor: "#000",//结束颜色
Step: 20//渐变级数
};
Extend(this.options, options || {});
},
//获取渐变颜色集合
Create: function() {
var colors = [],
startColor = this.GetColor(this.StartColor),
endColor = this.GetColor(this.EndColor),
stepR = (endColor[0] - startColor[0]) / this.Step,
stepG = (endColor[1] - startColor[1]) / this.Step,
stepB = (endColor[2] - startColor[2]) / this.Step;
//生成颜色集合
for(var i = 0, n = this.Step, r = startColor[0], g = startColor[1], b = startColor[2]; i < n; i++){
colors.push([r, g, b]); r += stepR; g += stepG; b += stepB;
}
colors.push(endColor);
//修正颜色值
return Map(colors, function(x){ return Map(x, function(x){
return Math.min(Math.max(0, Math.floor(x)), 255);
});});
},
//获取颜色数据
GetColor: function(color) {
if(/^#[0-9a-f]{6}$/i.test(color))
{//#rrggbb
return Map([color.substr(1, 2), color.substr(3, 2), color.substr(5, 2)],
function(x){ return parseInt(x, 16); }
)
}
else if(/^#[0-9a-f]{3}$/i.test(color))
{//#rgb
return Map([color.substr(1, 1), color.substr(2, 1), color.substr(3, 1)],
function(x){ return parseInt(x + x, 16); }
)
}
else if(/^rgb(.*)$/i.test(color))
{//rgb(n,n,n) or rgb(n%,n%,n%)
return Map(color.match(/\d+(\.\d+)?\%?/g),
function(x){ return parseInt(x.indexOf("%") > 0 ? parseFloat(x, 10) * 2.55 : x, 10); }
)
}
else
{//color
var mapping = {"red":"#FF0000"};//略
color = mapping[color.toLowerCase()];
if(color){
return Map([color.substr(1, 2), color.substr(3, 2), color.substr(5, 2)],
function(x){ return parseInt(x, 16); }
)
}
}
}
};
ColorTrans部分:
var ColorTrans = function(obj, options){
this._obj = $(obj);
this._timer = null;//定时器
this._index = 0;//索引
this._colors = [];//颜色集合
this._grads = new ColorGrads();
this.SetOptions(options);
this.Speed = Math.abs(this.options.Speed);
this.CssColor = this.options.CssColor;
this._startColor = this.options.StartColor || CurrentStyle(this._obj)[this.CssColor];
this._endColor = this.options.EndColor;
this._step = Math.abs(this.options.Step);
this.Reset();
this.SetColor();
};
ColorTrans.prototype = {
//设置默认属性
SetOptions: function(options) {
this.options = {//默认值
StartColor: "",//开始颜色
EndColor: "#000",//结束颜色
Step: 20,//渐变级数
Speed: 20,//渐变速度
CssColor: "color"//设置属性(Scripting属性)
};
Extend(this.options, options || {});
},
//重设颜色集合
Reset: function(color) {
//修改颜色后必须重新获取颜色集合
color = Extend({ StartColor: this._startColor, EndColor: this._endColor, Step: this._step }, color || {});
//设置属性
this._grads.StartColor = this._startColor = color.StartColor;
this._grads.EndColor = this._endColor = color.EndColor;
this._grads.Step = this._step = color.Step;
//获取颜色集合
this._colors = this._grads.Create();
this._index = 0;
},
//颜色渐入
FadeIn: function() {
this.Stop(); this._index++; this.SetColor();
if(this._index < this._colors.length - 1){
this._timer = setTimeout(Bind(this, this.FadeIn), this.Speed);
}
},
//颜色渐出
FadeOut: function() {
this.Stop(); this._index--; this.SetColor();
if(this._index > 0){
this._timer = setTimeout(Bind(this, this.FadeOut), this.Speed);
}
},
//颜色设置
SetColor: function() {
var color = this._colors[Math.min(Math.max(0, this._index), this._colors.length - 1)];
this._obj.style[this.CssColor] = "rgb(" + color[0] + "," + color[1] + "," + color[2] + ")";
},
//停止
Stop: function() {
clearTimeout(this._timer);
}
};
在线演示页面:Color.htm


猜你喜欢
- 一、装饰器的本质:装饰器(decorator)本质是函数闭包(function closure)的语法糖(Syntactic sugar)函
- python是3.6centos 6 64位1.安装python2.安装pipwget https://bootstrap.pypa.io/
- 一般我们的项目中会使用1到2个数据库连接配置,同程艺龙的数据库连接配置被收拢到统一的配置中心,由DBA统一配置和维护,业务方通过某个字符串配
- 扰动的鲁棒性在之前我们讨论权重衰减(L2正则化)时看到的那样,参数的范数也代表了一种有用的简单性度量。简单性的另一个有用角度是平滑性,即函数
- 引言“ 这是MySQL系列笔记的第二篇,文章内容均为本人通过实践及查阅资料相关整理所得,可用作新手入门指南,或
- Python中支持Convex Optimization(凸规划)的模块为CVXOPT,其安装方式为:pip install cvxopt一
- 本文实例讲述了python 并发下载器实现方法。分享给大家供大家参考,具体如下:并发下载器并发下载原理from gevent import
- 本文为多篇“HTTP请求头相关文章”及《HTTP权威指南》一书的阅读后个人汇总整理版,以便于理解。通常HTTP消息包括客户机向服务器的请求消
- 众所周知,OpenStack的通信方式有两种,一种是基于HTTP协议的RESTFul API方式,另一种则是RPC调用。两种通信方式的应用场
- 工欲善其事必先利其器,估计大部分的pythoner都是从python IDE开始了python之路。有很多的python IDE工具可以用,
- 前言SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:
- 本章我们将介绍Django 管理工具及如何使用 Django 来创建项目,第一个项目我们以 HelloWorld 来命令项目。一、Djang
- 判断字符串是否为空var strings = ''; if (string.length == 0) { alert(
- 一、web.py简介web.py是一款轻量级的Python web开发框架,简单、高效、学习成本低,特别适合作为python web开发的入
- 使用方式:new downUpData({url:"http://192.168.1.103:8080/test/data.jso
- PySnooper 是一个非常方便的调试器。如果您正在试图弄清楚为什么您的Python代码没有按照您的预期去做,您会希望使用具有断点和监视功
- 前言在这篇文章中,准备用 Python 从头开始实现一个全连接的神经网络。你可能会问,为什么需要自己实现,有很多库和框架可以为我们做这件事,
- mysql replace实例说明:UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'de
- 定义和用法strftime() 函数根据区域设置格式化本地时间/日期。语法strftime(format,timestamp)参数 描述 f
- 一下demo演示2.0中的vue-router是如何获取到不同参数的,并在地址栏中匹配不同的信息 <!DOCTYPE html>