Advertisement
Guest User

Luminance

a guest
Jul 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package citra;
  7.  
  8. import java.awt.Color;
  9.  
  10. /**
  11.  *
  12.  * @author Scope
  13.  */
  14. public class Luminance {
  15.  
  16.     // return the monochrome luminance of given color
  17.     @Deprecated
  18.     public static double lum(Color color) {
  19.         return intensity(color);
  20.     }
  21.  
  22.     // return the monochrome luminance of given color
  23.     public static double intensity(Color color) {
  24.         int r = color.getRed();
  25.         int g = color.getGreen();
  26.         int b = color.getBlue();
  27.         return 0.299*r + 0.587*g + 0.114*b;
  28.     }
  29.  
  30.     // return a gray version of this Color
  31.     public static Color toGray(Color color) {
  32.         int y = (int) (Math.round(lum(color)));   // round to nearest int
  33.         Color gray = new Color(y, y, y);
  34.         return gray;
  35.     }
  36.  
  37.     // are the two colors compatible?
  38.     public static boolean areCompatible(Color a, Color b) {
  39.         return Math.abs(intensity(a) - intensity(b)) >= 128.0;
  40.     }
  41.  
  42.     // test client
  43.     public static void main(String[] args) {
  44.         int[] a = new int[6];
  45.         for (int i = 0; i < 6; i++) {
  46.             a[i] = Integer.parseInt(args[i]);
  47.         }
  48.         Color c1 = new Color(a[0], a[1], a[2]);
  49.         Color c2 = new Color(a[3], a[4], a[5]);
  50.         System.out.println("c1 = " + c1);
  51.         System.out.println("c2 = " + c2);
  52.         System.out.println("intensity(c1) =  " + intensity(c1));
  53.         System.out.println("intensity(c2) =  " + intensity(c2));
  54.         System.out.println(areCompatible(c1, c2));
  55.     }
  56.    
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement