<p>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 <a href="http://jtauber.com/blog/2008/05/18/creating_gradients_programmatically_in_python/" rel="noreferrer">this blog entry</a>, 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.</p>

[{'answer_id': 27536, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 6, 'selected': True, 'text': '<p>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 <em>p</em> of the first colour and percentage 1 - <em>p</em> of the second:</p>\n\n<pre><code>R = firstCol.R * p + secondCol.R * (1 - p)\n</code></pre>\n\n<p>There\'s <a href="https://stackoverflow.com/questions/25007/conditional-formatting-percentage-to-color-conversion">another question</a> related to this.</p>\n\n<p>There are other methods of interpolation that sometimes work better. For example, using a <a href="http://en.wikipedia.org/wiki/Sigmoid_function" rel="noreferrer">bell-shaped (sigmoidal)</a> interpolation function makes the transition smoother.</p>\n\n<p>/EDIT: Oops, you mean using a predefined function. OK, even easier. The blog post you linked now has an example code in Python.</p>\n\n<p>In Java, you could use the <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/GradientPaint.html" rel="noreferrer"><code>GradientPaint</code></a>.</p>\n'}, {'answer_id': 27553, 'author': 'David Crow', 'author_id': 2783, 'author_profile': 'https://Stackoverflow.com/users/2783', 'pm_score': 3, 'selected': False, 'text': '<p>Using the basic AWT classes, you could do something like this:</p>\n\n<pre><code>import java.awt.Color;\nimport java.awt.Graphics;\nimport java.awt.Graphics2D;\nimport java.awt.geom.Rectangle2D;\nimport javax.swing.JPanel;\n\npublic class LinearGradient extends JPanel {\n\n    public void paint(Graphics g) {\n        Graphics2D g2 = (Graphics2D) g;\n        Color color1 = Color.RED;\n        Color color2 = Color.BLUE;\n        int steps = 30;\n        int rectWidth = 10;\n        int rectHeight = 10;\n\n        for (int i = 0; i &lt; steps; i++) {\n            float ratio = (float) i / (float) steps;\n            int red = (int) (color2.getRed() * ratio + color1.getRed() * (1 - ratio));\n            int green = (int) (color2.getGreen() * ratio + color1.getGreen() * (1 - ratio));\n            int blue = (int) (color2.getBlue() * ratio + color1.getBlue() * (1 - ratio));\n            Color stepColor = new Color(red, green, blue);\n            Rectangle2D rect2D = new Rectangle2D.Float(rectWidth * i, 0, rectWidth, rectHeight);\n            g2.setPaint(stepColor);\n            g2.fill(rect2D);\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 27561, 'author': 'dbkk', 'author_id': 838, 'author_profile': 'https://Stackoverflow.com/users/838', 'pm_score': 4, 'selected': False, 'text': '<p>You can use the built in <a href="https://docs.oracle.com/javase/7/docs/api/java/awt/GradientPaint.html" rel="nofollow noreferrer">GradientPaint</a> class. </p>\n\n<pre><code>void Paint(Graphics2D g, Regtangle r, Color c1, Color c2)\n{\n  GradientPaint gp = new GradientPaint(0,0,c1,r.getWidth(),r.getHeight(),c2); \n  g.setPaint(gp);\n  g.fill(rect);\n}\n</code></pre>\n'}, {'answer_id': 221413, 'author': 'Thibaut Barrère', 'author_id': 20302, 'author_profile': 'https://Stackoverflow.com/users/20302', 'pm_score': 0, 'selected': False, 'text': '<p>I\'ve been using <a href="http://blog.logeek.fr/2008/10/21/generating-gradients-for-your-css-using-rmagick" rel="nofollow noreferrer">RMagick for that</a>. If you need to go further the simple gradient, ImageMagick and one of its wrappers (like RMagick or <a href="http://sourceforge.net/projects/jmagick/" rel="nofollow noreferrer">JMagick</a> for Java) could be useful.</p>\n'}, {'answer_id': 47324548, 'author': 'Holger Brandl', 'author_id': 590437, 'author_profile': 'https://Stackoverflow.com/users/590437', 'pm_score': 2, 'selected': False, 'text': "<p>Following up on the execllent answer of David Crow, here's a Kotlin example implementation</p>\n\n<pre><code>fun gradientColor(x: Double, minX: Double, maxX: Double, \n                  from: Color = Color.RED, to: Color = Color.GREEN): Color {\n    val range = maxX - minX\n    val p = (x - minX) / range\n\n   return Color(\n        from.red * p + to.red * (1 - p),\n        from.green * p + to.green * (1 - p),\n        from.blue * p + to.blue * (1 - p),\n        1.0\n    )\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/27532', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/85/']