以编程方式生成梯度? [英] Generating gradients programmatically?

查看:33
本文介绍了以编程方式生成梯度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定 2 种 rgb 颜色和一个矩形区域,我想在颜色之间生成一个基本的线性渐变.我进行了快速搜索,唯一能找到的是 this博客条目,但示例代码似乎丢失了,或者至少在这篇文章中是这样.任何帮助,算法,代码示例,等等.这个会用Java写的,但是显示层已经处理好了,我只需要弄清楚如何弄清楚要显示的内容.

Given 2 rgb colors and a rectangular area, I'd like to generate a basic linear gradient between the colors. I've done a quick search and the only thing I've been able to find is this blog entry, but the example code seems to be missing, or at least it was as of this posting. Anything helps, algorithms, code examples, whatever. This will be written in Java, but the display layer is already taken care of, I just need to figure out how to figure out what to display.

推荐答案

您想要在第一种颜色和第二种颜色之间进行插值.通过为其每个分量(R、G、B)计算相同的插值,插值颜色很容易.有多种插值方法.最简单的方法是使用线性插值:只需取第一种颜色的百分比 p 和第二种颜色的百分比 1 - p:

you want an interpolation between the first and the second colour. Interpolating colours is easy by calculating the same interpolation for each of its components (R, G, B). There are many ways to interpolate. The easiest is to use linear interpolation: just take percentage p of the first colour and percentage 1 - p of the second:

R = firstCol.R * p + secondCol.R * (1 - p)

还有与此相关的另一个问题.

还有其他插值方法有时效果更好.例如,使用 钟形(sigmoidal) 插值函数可使过渡更平滑.

There are other methods of interpolation that sometimes work better. For example, using a bell-shaped (sigmoidal) interpolation function makes the transition smoother.

/糟糕,您的意思是使用预定义的函数.好吧,更简单了.您链接的博文现在有一个 Python 示例代码.

/ Oops, you mean using a predefined function. OK, even easier. The blog post you linked now has an example code in Python.

在 Java 中,您可以使用 GradientPaint.

In Java, you could use the GradientPaint.

这篇关于以编程方式生成梯度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆