]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/color/RGBAColorFilter.java
RGBA color filter
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / color / RGBAColorFilter.java
1 /*******************************************************************************
2  * Copyright (c) 2020 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.g2d.color;
13
14 import java.awt.Color;
15
16 public class RGBAColorFilter implements ColorFilter {
17
18     private double r, g, b, a;
19     
20     public RGBAColorFilter(double r, double g, double b, double a) {
21         this.r = r;
22         this.g = g;
23         this.b = b;
24         this.a = a;
25     }
26
27     @Override
28     public Color filter(Color c) {
29         int avg = (c.getRed() + c.getGreen() + c.getBlue()) / 3; 
30         int R = Math.min(255, (int)(avg * r));
31         int G = Math.min(255, (int)(avg * g));
32         int B = Math.min(255, (int)(avg * b));
33         int A = Math.min(255, (int)(c.getAlpha() * a));
34         Color c2 = new Color(R, G, B, A);
35         return c2;
36     }
37
38     @Override
39     public int hashCode() {
40         final int prime = 31;
41         int result = 1;
42         long temp;
43         temp = Double.doubleToLongBits(a);
44         result = prime * result + (int) (temp ^ (temp >>> 32));
45         temp = Double.doubleToLongBits(b);
46         result = prime * result + (int) (temp ^ (temp >>> 32));
47         temp = Double.doubleToLongBits(g);
48         result = prime * result + (int) (temp ^ (temp >>> 32));
49         temp = Double.doubleToLongBits(r);
50         result = prime * result + (int) (temp ^ (temp >>> 32));
51         return result;
52     }
53
54     @Override
55     public boolean equals(Object obj) {
56         if (this == obj)
57             return true;
58         if (obj == null)
59             return false;
60         if (getClass() != obj.getClass())
61             return false;
62         RGBAColorFilter other = (RGBAColorFilter) obj;
63         if (Double.doubleToLongBits(a) != Double.doubleToLongBits(other.a))
64             return false;
65         if (Double.doubleToLongBits(b) != Double.doubleToLongBits(other.b))
66             return false;
67         if (Double.doubleToLongBits(g) != Double.doubleToLongBits(other.g))
68             return false;
69         if (Double.doubleToLongBits(r) != Double.doubleToLongBits(other.r))
70             return false;
71         return true;
72     }
73 }