Generating gradients programmatically?

前端 未结 5 806
深忆病人
深忆病人 2020-12-02 09:16

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

5条回答
  •  情深已故
    2020-12-02 10:15

    Following up on the execllent answer of David Crow, here's a Kotlin example implementation

    fun gradientColor(x: Double, minX: Double, maxX: Double, 
                      from: Color = Color.RED, to: Color = Color.GREEN): Color {
        val range = maxX - minX
        val p = (x - minX) / range
    
       return Color(
            from.red * p + to.red * (1 - p),
            from.green * p + to.green * (1 - p),
            from.blue * p + to.blue * (1 - p),
            1.0
        )
    }
    

提交回复
热议问题