iOS Find Color at Point Between Two Colors

后端 未结 5 804
感情败类
感情败类 2020-12-05 06:00

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 cu

5条回答
  •  孤街浪徒
    2020-12-05 06:11

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

    But let's simplify.

    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];
    }
    

提交回复
热议问题