iOS在两种颜色之间的点处查找颜色 [英] iOS Find Color at Point Between Two Colors

查看:234
本文介绍了iOS在两种颜色之间的点处查找颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题:我需要能够采用两种颜色并从中制作出虚拟渐变。然后我需要能够在这一行的任何一点找到颜色。我目前的做法是:

I have a problem: I need to be able to take two colors and make a 'virtual gradient' out of them. I then need to be able to find the color at any point on this line. My current approach is this:

if (fahrenheit < kBottomThreshold)
{
    return [UIColor colorWithRed:kBottomR/255.0f green:kBottomG/255.0f blue:kBottomB/255.0f alpha:1];
}
if (fahrenheit > kTopThreshold)
{
    return [UIColor colorWithRed:kTopR/255.0f green:kTopG/255.0f blue:kTopB/255.0f alpha:1];
}

double rDiff = kTopR - kBottomR;
double gDiff = kTopG - kBottomG;
double bDiff = kTopB - kBottomB;

double tempDiff = kTopThreshold - kBottomThreshold;

double rValue;
double gValue;
double bValue;

rValue = kBottomR + ((rDiff/tempDiff) * fahrenheit);
gValue = kBottomG + ((gDiff/tempDiff) * fahrenheit);
bValue = kBottomB + ((bDiff/tempDiff) * fahrenheit);

return [UIColor colorWithRed:rValue/255.0f green:gValue/255.0f blue:bValue/255.0f alpha:1];

变量:


  • fahrenheit 是一个传递给我的函数的变量,它是我要为其找到颜色的虚拟行上的数字。

  • kTopR kTopB kTopG 是RGB渐变的一端的值。他们的 kBottom 同行相同。

  • kBottomThreshold kTopThreshold 是渐变的终点。

  • fahrenheit is a variable passed into my function that is the number on this virtual line that I want to find the color for.
  • kTopR, kTopB, and kTopG are the RGB values for one end of the gradient. Same for their kBottom counterparts.
  • kBottomThreshold and kTopThreshold are the endpoints of my gradient.

这是我的问题: fahrenheit 越过渐变的任一端时,渐变似乎跳到不同的值。

Here's my problem: When fahrenheit goes over either end of the gradient, the gradient seems to 'jump' to a different value.

我已经在我的S3服务器上托管了一个示例项目,这里

I've included an example project, hosted on my S3 server, here.

您真的需要下载项目并在模拟器/设备上尝试看看我的意思 (除非你很聪明,只能通过查看代码来判断)

You really need to download the project and try it on the simulator/device to see what I mean (unless you are crazy smart and can tell just by looking at the code)

推荐答案

问题是你没有从 farenheit 中减去 kBottomThreshold

The problem is that you're not subtracting kBottomThreshold from farenheit.

但是让我们简化。

F.首先,我们要将输入温度映射到[0 ... 1]范围内的参数 t 。然后,我们要将 t 映射到[ kBottomR ... 范围内的输出kTopR ],以及[ kBottomG ... kTopG 范围内的输出],以及[ kBottomB ... kTopB ]范围内的输出。

First, we want to map the input temperature to a parameter t in the range [0 ... 1]. Then, we want to map t to an output in the range [kBottomR ... kTopR], and also to an output in the range [kBottomG ... kTopG], and also to an output in the range [kBottomB ... kTopB].

UIColor *colorForDegreesFahrenheit(double fahrenheit) {
    double t = (fahrenheit - kBottomThreshold) / (kTopThreshold - kBottomThreshold);

    // Clamp t to the range [0 ... 1].
    t = MAX(0.0, MIN(t, 1.0));

    double r = kBottomR + t * (kTopR - kBottomR);
    double g = kBottomG + t * (kTopG - kBottomG);
    double b = kBottomB + t * (kTopB - kBottomB);

    return [UIColor colorWithRed:r/255 green:g/255 blue:b/255 alpha:1];
}

这篇关于iOS在两种颜色之间的点处查找颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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