Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.impl / src / main / java / org / gvsig / fmap / raster / lib / legend / impl / ColorUtils.java @ 44831

History | View | Annotate | Download (4.16 KB)

1
package org.gvsig.raster.lib.legend.impl;
2

    
3
import java.awt.Color;
4

    
5
/**
6
 * Utility class to color conversions
7
 * @author dmartinezizquierdo
8
 *
9
 */
10
public class ColorUtils {
11

    
12
    /**
13
     *
14
     * @param y
15
     * @param cb
16
     * @param cr
17
     * @return the RGB values
18
     */
19
    public static Number[] fromYCBCRtoRGB(float y, float cb, float cr){
20
        Number[] rgbValues = new Number[3];
21

    
22
        int r = (int) (y + 1.40200 * (cr - 0x80));
23
        int g = (int) (y - 0.34414 * (cb - 0x80) - 0.71414 * (cr - 0x80));
24
        int b = (int) (y + 1.77200 * (cb - 0x80));
25

    
26
        rgbValues[0] = Math.max(0, Math.min(255, r));
27
        rgbValues[1] = Math.max(0, Math.min(255, g));
28
        rgbValues[2] = Math.max(0, Math.min(255, b));
29

    
30
        return rgbValues;
31
    }
32

    
33
    /**
34
     * Convert CMYK values to RGB values
35
     * NOTE: Conversion maybe very inaccurate
36
     *
37
     * @param cyan
38
     * @param magenta
39
     * @param yellow
40
     * @param black
41
     * @return the RGB values
42
     */
43
    public static Number[] fromCMYKtoRGB(float cyan, float magenta, float yellow, float black){
44
        //TODO: ICC profiles should be used to more precise conversion, as shown here:
45
        //http://stackoverflow.com/questions/3123574/how-to-convert-from-cmyk-to-rgb-in-java-correctly
46
        Number[] rgbValues = new Number[3];
47

    
48
        float colors = 255 - black;
49
        rgbValues[0] = colors * (255 - cyan) / 255;
50
        rgbValues[1] = colors * (255 - magenta) / 255;
51
        rgbValues[2] = colors * (255 - yellow) / 255;
52
        return rgbValues;
53
    }
54

    
55
    /**
56
     *  Convert HSL values to RGBA values with a default alpha value of 1.
57
     *
58
     *  @param h Hue is specified as degrees in the range 0 - 360.
59
     *  @param s Saturation is specified as a percentage in the range 1 - 100.
60
     *  @param l Lumanance is specified as a percentage in the range 1 - 100.
61
     *
62
     *  @returns the RGBA values
63
     */
64
    public static Number[] fromHSLtoRGBA(float h, float s, float l)
65
    {
66
        return fromHSLtoRGBA(h, s, l, 1.0f);
67
    }
68

    
69
    /**
70
     *  Convert HSL values to RGBA values
71
     *
72
     *  @param h Hue is specified as degrees in the range 0 - 360.
73
     *  @param s Saturation is specified as a percentage in the range 1 - 100.
74
     *  @param l Lumanance is specified as a percentage in the range 1 - 100.
75
     *  @param alpha  the alpha value between 0 - 1
76
     *
77
     *  @returns the RGBA values
78
     */
79
    public static Number[] fromHSLtoRGBA(float h, float s, float l, float alpha)
80
    {
81
        if (s <0.0f || s > 100.0f)
82
        {
83
            String message = "Color parameter outside of expected range - Saturation";
84
            throw new IllegalArgumentException( message );
85
        }
86

    
87
        if (l <0.0f || l > 100.0f)
88
        {
89
            String message = "Color parameter outside of expected range - Luminance";
90
            throw new IllegalArgumentException( message );
91
        }
92

    
93
        if (alpha <0.0f || alpha > 1.0f)
94
        {
95
            String message = "Color parameter outside of expected range - Alpha";
96
            throw new IllegalArgumentException( message );
97
        }
98

    
99
        //  Formula needs all values between 0 - 1.
100

    
101
        h = h % 360.0f;
102
        h /= 360f;
103
        s /= 100f;
104
        l /= 100f;
105

    
106
        float q = 0;
107

    
108
        if (l < 0.5)
109
            q = l * (1 + s);
110
        else
111
            q = (l + s) - (s * l);
112

    
113
        float p = 2 * l - q;
114

    
115
        float r = Math.max(0, HueToRGB(p, q, h + (1.0f / 3.0f)));
116
        float g = Math.max(0, HueToRGB(p, q, h));
117
        float b = Math.max(0, HueToRGB(p, q, h - (1.0f / 3.0f)));
118

    
119
        r = Math.min(r, 1.0f);
120
        g = Math.min(g, 1.0f);
121
        b = Math.min(b, 1.0f);
122

    
123
        Number[] rgbValues=new Number[4];
124
        rgbValues[0]=r;
125
        rgbValues[1]=g;
126
        rgbValues[2]=b;
127
        rgbValues[3]=alpha;
128
        return rgbValues;
129
    }
130

    
131
    private static float HueToRGB(float p, float q, float h)
132
    {
133
        if (h < 0) h += 1;
134

    
135
        if (h > 1 ) h -= 1;
136

    
137
        if (6 * h < 1)
138
        {
139
            return p + ((q - p) * 6 * h);
140
        }
141

    
142
        if (2 * h < 1 )
143
        {
144
            return  q;
145
        }
146

    
147
        if (3 * h < 2)
148
        {
149
            return p + ( (q - p) * 6 * ((2.0f / 3.0f) - h) );
150
        }
151

    
152
        return p;
153
    }
154
}