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   */
25  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
26  public class Color {
27      private final int red_;
28      private final int green_;
29      private final int blue_;
30      private final int alpha_;
31  
32      /**
33       * Ctor (using an alpha of 255).
34       *
35       * @param red the red part
36       * @param green the green part
37       * @param blue the blue part
38       */
39      public Color(final int red, final int green, final int blue) {
40          this(red, green, blue, 255);
41      }
42  
43      /**
44       * Ctor.
45       *
46       * @param red the red part
47       * @param green the green part
48       * @param blue the blue part
49       * @param alpha the alpha part
50       */
51      public Color(final int red, final int green, final int blue, final int alpha) {
52          if (red < 0 || red > 255) {
53              throw new IllegalArgumentException("Color red value '" + red + " outside of expected range");
54          }
55          if (green < 0 || green > 255) {
56              throw new IllegalArgumentException("Color green value '" + green + " outside of expected range");
57          }
58          if (blue < 0 || blue > 255) {
59              throw new IllegalArgumentException("Color blue value '" + blue + " outside of expected range");
60          }
61          if (alpha < 0 || alpha > 255) {
62              throw new IllegalArgumentException("Color red value '" + alpha + " outside of expected range");
63          }
64  
65          red_ = red;
66          green_ = green;
67          blue_ = blue;
68          alpha_ = alpha;
69      }
70  
71      /**
72       * @return the red part
73       */
74      public int getRed() {
75          return red_;
76      }
77  
78      /**
79       * @return the green part
80       */
81      public int getGreen() {
82          return green_;
83      }
84  
85      /**
86       * @return the blue part
87       */
88      public int getBlue() {
89          return blue_;
90      }
91  
92      /**
93       * @return the alpha part
94       */
95      public int getAlpha() {
96          return alpha_;
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public int hashCode() {
104         return Objects.hash(red_, green_, blue_, alpha_);
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public boolean equals(final Object obj) {
112         if (this == obj) {
113             return true;
114         }
115         if (obj == null) {
116             return false;
117         }
118         if (getClass() != obj.getClass()) {
119             return false;
120         }
121 
122         final Color other = (Color) obj;
123         if (alpha_ != other.alpha_) {
124             return false;
125         }
126         if (blue_ != other.blue_) {
127             return false;
128         }
129         if (green_ != other.green_) {
130             return false;
131         }
132         if (red_ != other.red_) {
133             return false;
134         }
135         return true;
136     }
137 }