以编程方式生成渐变? [英] Generating gradients programmatically?

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

问题描述

给定2 rgb颜色和一个矩形区域,我想在颜色之间生成一个基本的线性渐变。我做了一个快速搜索,我唯一能找到的是这个博客条目,但示例代码似乎丢失,或至少它是这个发布。任何帮助,算法,代码示例,无论什么。这将用Java编写,但显示层已经被照顾,我只需要弄清楚如何找出显示什么。

解决方案

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

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

有与此相关的另一个问题



有其他插值方法有时工作得更好。例如,使用钟形(Sigmoidal)插值函数可以使过渡变得更平滑。 p>

/ EDIT:糟糕,您的意思是使用预定义的函数。 OK,更容易。您链接的博客现在有一个Python示例代码。



在Java中,您可以使用 GradientPaint


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.

解决方案

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)

There's another question related to this.

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

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

In Java, you could use the GradientPaint.

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

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