Download presentation
Presentation is loading. Please wait.
1
Computer Programming Methodology Luminance
COS 130 Computer Programming Methodology Luminance
2
More about color or lack there of Luminance
3
Luminance Used to represent color as shades of grey
The eye sees different colors with more or less intensity Therefore we weight different colors when converting them to grey
4
Luminance We will use this formula
to go from RGB to luminance (gray scale) red * green * blue * 0.11
5
Luminance Formula in Java
int red; int green; int blue; int gs; . . . gs = red * green * blue * 0.11; What happens here? Promoted to double int double Truncate!
6
A cast (in this case to an int)
Casting Primitives Tells the compiler – Just Trust Me, I know what I’m doing A cast (in this case to an int) gs = (int) (red * green * blue * 0.11);
7
When to use Promotion Casting Put a small thing into a big thing
Generally safe Happens automatically Casting Put a big thing into a small thing You must code May result in errors Example int n = (int) 2.34
8
Rounding When we truncate we would really like to round to the closest integer int num = (int)3.999; In this example we would like num to end up as 4 not 3 So we add .5 int num = (int) ( );
9
Final Version Luminance in Java
gs = (int) (red * green * blue * );
10
What values are allowed?
gs = (int) (red * green * blue * ); System.out.write(gs); 0 through 255 public static int clamp(int n) { if(n < 0) return 0; if(n > 255) return 255; return n; } gs = (int) (red * green * blue * ); gs = clamp(gs); System.out.write(gs);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.