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