生成颜色渐变

发布于 2024-08-01 18:51:57 字数 178 浏览 9 评论 0原文

我有一个想法以编程方式生成匹配的配色方案,但是我需要能够在给定一组两种颜色(十六进制或 RGB 值)的情况下生成线性渐变。

任何人都可以向我提供(伪)代码或为我指明完成此任务的正确方向吗?

编辑:我忘了提及,但我还需要指定(或知道)渐变从颜色 A 到颜色 B 的步数。

I had an idea to programmatically generate matching color schemes however I need to be able to generate a linear gradient given a set of two colors (Hex or RGB values).

Can anyone provide me the (pseudo-)code or point me in the right direction to accomplish this task?

EDIT: I forgot to mention, but I also need to specify (or know) the number of steps the gradient takes from color A to color B.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不及他 2024-08-08 18:51:57

好的,你知道步骤了,开始颜色和结束颜色。 假设每种颜色都有 RGB 值:

   red_diff = end_red - start_red
   green_diff = end_green - start_green
   blue_diff = end_blue - start_blue

   #Note: This is all integer division
   red_step = red_diff / num_steps 
   green_step = green_diff / num_steps
   blue_step = blue_diff / num_steps

   current_red = start_red
   current_geen = start_green
   current_blue = start_blue

   while current_red != end_red and current_green != end_green and current_blue != end_blue:
       current_red += red_step
       current_green += green_step
       current_blue += blue_step
       # print color

Okay, so you know the steps, start color and end color. Assuming you have RGB values for each color:

   red_diff = end_red - start_red
   green_diff = end_green - start_green
   blue_diff = end_blue - start_blue

   #Note: This is all integer division
   red_step = red_diff / num_steps 
   green_step = green_diff / num_steps
   blue_step = blue_diff / num_steps

   current_red = start_red
   current_geen = start_green
   current_blue = start_blue

   while current_red != end_red and current_green != end_green and current_blue != end_blue:
       current_red += red_step
       current_green += green_step
       current_blue += blue_step
       # print color
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文