• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Smooth transition between 2 (hexadecimal) colours

 
Ranch Hand
Posts: 46
  • 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.
 
Bartender
Posts: 7431
66
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
Bartender
Posts: 7431
66
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: 46
    • 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
     
    Marshal
    Posts: 71042
    291
    • 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: 12488
    269
    • 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
    Bartender
    Posts: 7431
    66
    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.
     
    Saloon Keeper
    Posts: 4153
    160
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    here's a method that I use myself:
     
    Carey Brown
    Bartender
    Posts: 7431
    66
    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.
     
    Space pants. Tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic