View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html.impl;
16  
17  import java.util.Objects;
18  
19  /**
20   * Our own implementation of color to be
21   * independent of awt (for this).
22   *
23   * @author Ronald Brill
24   * @author Ashutosh Panda
25   */
26  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
27  public class Color {
28      private final int red_;
29      private final int green_;
30      private final int blue_;
31      private final int alpha_;
32  
33      /**
34       * Ctor (using an alpha of 255).
35       *
36       * @param red the red part
37       * @param green the green part
38       * @param blue the blue part
39       */
40      public Color(final int red, final int green, final int blue) {
41          this(red, green, blue, 255);
42      }
43  
44      /**
45       * Ctor.
46       *
47       * @param red the red part
48       * @param green the green part
49       * @param blue the blue part
50       * @param alpha the alpha part
51       */
52      public Color(final int red, final int green, final int blue, final int alpha) {
53          if (red < 0 || red > 255) {
54              throw new IllegalArgumentException("Color red value '" + red + " outside of expected range");
55          }
56          if (green < 0 || green > 255) {
57              throw new IllegalArgumentException("Color green value '" + green + " outside of expected range");
58          }
59          if (blue < 0 || blue > 255) {
60              throw new IllegalArgumentException("Color blue value '" + blue + " outside of expected range");
61          }
62          if (alpha < 0 || alpha > 255) {
63              throw new IllegalArgumentException("Color red value '" + alpha + " outside of expected range");
64          }
65  
66          red_ = red;
67          green_ = green;
68          blue_ = blue;
69          alpha_ = alpha;
70      }
71  
72      /**
73       * Returns the red part.
74       *
75       * @return the red part
76       */
77      public int getRed() {
78          return red_;
79      }
80  
81      /**
82       * Returns the green part.
83       *
84       * @return the green part
85       */
86      public int getGreen() {
87          return green_;
88      }
89  
90      /**
91       * Returns the blue part.
92       *
93       * @return the blue part
94       */
95      public int getBlue() {
96          return blue_;
97      }
98  
99      /**
100      * Returns the alpha part.
101      *
102      * @return the alpha part
103      */
104     public int getAlpha() {
105         return alpha_;
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public int hashCode() {
113         return Objects.hash(red_, green_, blue_, alpha_);
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public boolean equals(final Object obj) {
121         if (this == obj) {
122             return true;
123         }
124         if (obj == null) {
125             return false;
126         }
127         if (getClass() != obj.getClass()) {
128             return false;
129         }
130 
131         final Color other = (Color) obj;
132         if (alpha_ != other.alpha_) {
133             return false;
134         }
135         if (blue_ != other.blue_) {
136             return false;
137         }
138         if (green_ != other.green_) {
139             return false;
140         }
141         return red_ == other.red_;
142     }
143 }