• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Smooth transition between 2 (hexadecimal) colours

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

I am trying to get a smooth transition between two colours using 2 hexadecimal values. The code I use to do that is this;



this updates 60 times a second, and works. Then the code to calculate the actual colour to draw;



the code all works but something weird happens. When I go, for example, from 0xff_383838 to 0xff_c8c8c8 (those are both 2 different grey colours) in 20 steps, the colours in between are not at all grey. They are purple, orange, green etc. Eventually it ends on the end colour I want. So the last colour is as I want it to be but the colours in between are not the right colours. Does anyone know why this happens and how to fix this? When I entered this colours with the in between steps on a color blender website, the colours in between are all grey, like I want them to be.
 
Saloon Keeper
Posts: 6243
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are computing an interpolated color number which includes red, green, and blue all in one integer value. What you need to do is break out each of the RGB values separately and interpolate them separately and finally combine them back into a Color value.
 
Carey Brown
Saloon Keeper
Posts: 6243
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some pointers:
  • Don't write long lines, they are difficult for people to read. Break them up across multiple lines.
  • Don't use 'float', use 'double' instead. Not only does it have more precision but is handled more efficiently on many platforms.
  •  
    Luc Drenth
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you so much for helping! And for the tip on writing some cleaner code
     
    Carey Brown
    Saloon Keeper
    Posts: 6243
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
     
    Marshal
    Posts: 65814
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There might be something in the Java™ Tutorials about that, but I am not certain.
     
    Saloon Keeper
    Posts: 10669
    228
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't use int to represent your colors. Use Color instead. You might also want to use HSB values instead of RGB values, because you can control the brightness individually from the other components.
     
    Carey Brown
    Saloon Keeper
    Posts: 6243
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Interesting note on HSB.

    Luc, can you confirm if you are varying brightness only or do you also vary the color.
     
    Bartender
    Posts: 3519
    150
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    here's a method that I use myself:
     
    Carey Brown
    Saloon Keeper
    Posts: 6243
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here's my more verbose attempt. My goal here was to pre-calculate everything possible so that the processing necessary for each step was keep to a minimum. Luc mentioned updating at 60 frames per second but I suspect that's not what it actually works out to be.
     
    My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!