html渐变色原理,HTML5 渐变颜色样本

本文介绍了一种从RYB颜色空间转换到RGB颜色空间的方法,并通过JavaScript实现了该转换过程。此外,还提供了颜色渐变及不同缓动效果的演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

function cubicInt(t, A, B) {

var weight = t * t * (3 - 2 * t);

return A + weight * (B - A);

}

function getR(iR, iY, iB) {

// red

var x0 = cubicInt(iB, 1.0, 0.163);

var x1 = cubicInt(iB, 1.0, 0.0);

var x2 = cubicInt(iB, 1.0, 0.5);

var x3 = cubicInt(iB, 1.0, 0.2);

var y0 = cubicInt(iY, x0, x1);

var y1 = cubicInt(iY, x2, x3);

return Math.ceil(255 * cubicInt(iR, y0, y1));

}

function getG(iR, iY, iB) {

// green

var x0 = cubicInt(iB, 1.0, 0.373);

var x1 = cubicInt(iB, 1.0, 0.66);

var x2 = cubicInt(iB, 0.0, 0.0);

var x3 = cubicInt(iB, 0.5, 0.094);

var y0 = cubicInt(iY, x0, x1);

var y1 = cubicInt(iY, x2, x3);

return Math.ceil(255 * cubicInt(iR, y0, y1));

}

function getB(iR, iY, iB) {

// blue

var x0 = cubicInt(iB, 1.0, 0.6);

var x1 = cubicInt(iB, 0.0, 0.2);

var x2 = cubicInt(iB, 0.0, 0.5);

var x3 = cubicInt(iB, 0.0, 0.0);

var y0 = cubicInt(iY, x0, x1);

var y1 = cubicInt(iY, x2, x3);

return Math.ceil(255 * cubicInt(iR, y0, y1));

}

function ryb2rgb(color) {

var R = color[0] / 255;

var Y = color[1] / 255;

var B = color[2] / 255;

var R1 = getR(R, Y, B);

var G1 = getG(R, Y, B);

var B1 = getB(R, Y, B);

var ret = [R1, G1, B1];

return ret;

}

function cycle3(steps, callback) {

var a, b, c, i;

var step = 1 / steps;

i = 0;

a = 1;

b = 0;

c = 0;

while (i < steps) {

i++;

callback(a, b, c);

a = a - step;

b = b + step;

}

i = 0;

a = 0;

b = 1;

c = 0;

while (i < steps) {

i++;

callback(a, b, c);

b = b - step;

c = c + step;

}

i = 0;

a = 0;

b = 0;

c = 1;

while (i < steps) {

i++;

callback(a, b, c);

c = c - step;

a = a + step;

}

}

function easeNone(component) {

return component;

}

function easeSaw(component) {

component = Math.min(component * 2, 1);

return component;

}

function easeSin(component) {

return Math.sin(component * (Math.PI / 2));

}

function easeSawSin(component) {

component = Math.min(Math.sin(component * (Math.PI / 2)) * 2, 1);

return component;

}

function make255(component) {

return Math.floor(component * 255);

}

function addSwatch(container, r, g, b) {

var swatch = $("

var rgb = "rgb(" + [r, g, b].join(",") + ")";

swatch.css("backgroundColor", rgb);

container.append(swatch);

}

function addRow(easing) {

var container = $("

").appendTo($("#page"));

cycle3(24, function(a, b, c) {

var color = [a, b, c].map(easing);

color = ryb2rgb(color.map(make255));

addSwatch.apply(this, [container].concat(color));

});

}

function easeTest(component) {

value = easeSaw(component);

value = value + ((1 - value) * .2);

return value;

}

function linearDarken(amount, component) {

return component + ((1 - component) * amount);

}

function sin(unitValue) {

return Math.sin(unitValue * (Math.PI / 2));

}

function clip(value) {

return Math.max(Math.min(value, 1), 0);

}

function sinDarken(amount, component) {

var amt = ((1 - component) * amount);

amt = Math.sin(amt);

return component + amt;

}

addRow(function(x) {

return linearDarken(0.75, easeSin(x));

});

addRow(easeSin);

addRow(function(x) {

return sinDarken(0.75, easeSin(x));

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值