~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/contrib/randelshofer/quaqua/colorchooser/HTMLColorSliderModel.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * @(#)HTMLColorSliderModel.java  1.0.1  2005-08-28
 
3
 *
 
4
 * Copyright (c) 2005 Werner Randelshofer
 
5
 * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
 
6
 * All rights reserved.
 
7
 *
 
8
 * This software is the confidential and proprietary information of
 
9
 * Werner Randelshofer. ("Confidential Information").  You shall not
 
10
 * disclose such Confidential Information and shall use it only in
 
11
 * accordance with the terms of the license agreement you entered into
 
12
 * with Werner Randelshofer.
 
13
 */
 
14
 
 
15
package org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.colorchooser;
 
16
 
 
17
import javax.swing.*;
 
18
/**
 
19
 * ColorSliderModel for the HTML color model (red, green, blue, restricted
 
20
 * to values considered as web-save).
 
21
 *
 
22
 * @author  Werner Randelshofer
 
23
 * @version 1.0.1 2005-08-28 Method toWebSave generates now ARGB value instead
 
24
 * of just an RGB value. Method isWebSave ignores the alpha channel of a color.
 
25
 * <br>1.0 May 22, 2005 Created.
 
26
 */
 
27
public class HTMLColorSliderModel extends RGBColorSliderModel {
 
28
    private boolean isWebSaveOnly = true;
 
29
    /**
 
30
     * Creates a new instance.
 
31
     */
 
32
    public HTMLColorSliderModel() {
 
33
    }
 
34
    
 
35
    @Override
 
36
    public int getRGB() {
 
37
        return getRGB(components[0].getValue(), components[1].getValue(), components[2].getValue());
 
38
    }
 
39
    @Override
 
40
    public int getInterpolatedRGB(int component, float value) {
 
41
        if (isWebSaveOnly) {
 
42
            for (int i=0, n = getComponentCount(); i < n; i++) {
 
43
                values[i] = Math.round(components[i].getValue() / 51f) * 51;
 
44
            }
 
45
            values[component] = Math.round((value * components[component].getMaximum()) / 51f) * 51;
 
46
            return toRGB(values);
 
47
        } else {
 
48
            return super.getInterpolatedRGB(component, value);
 
49
        }
 
50
    }
 
51
    @Override
 
52
    protected int getRGB(int r, int g, int b) {
 
53
        if (isWebSaveOnly) {
 
54
            return 0xff000000 | (Math.round(r / 51f) * 51) << 16 | (Math.round(g / 51f) * 51) << 8 | Math.round(b / 51f) * 51;
 
55
        } else {
 
56
            return super.getRGB(r, g, b);
 
57
        }
 
58
    }
 
59
    
 
60
    @Override
 
61
    public void setRGB(int rgb) {
 
62
        if (isWebSaveOnly) {
 
63
            components[0].setValue((Math.round((rgb & 0xff0000) / 51f) * 51) >> 16);
 
64
            components[1].setValue((Math.round((rgb & 0x00ff00) / 51f) * 51) >> 8);
 
65
            components[2].setValue(Math.round((rgb & 0x0000ff) / 51f) * 51);
 
66
        } else {
 
67
            super.setRGB(rgb);
 
68
        }
 
69
    }
 
70
    
 
71
    @Override
 
72
    public int toRGB(int[] values) {
 
73
        if (isWebSaveOnly) {
 
74
            return 0xff000000 
 
75
            | (Math.round(values[0] / 51f) * 51) << 16 
 
76
            | (Math.round(values[1] / 51f) * 51) << 8 
 
77
            | (Math.round(values[2] / 51f) * 51);
 
78
        } else {
 
79
            return super.toRGB(values);
 
80
        }
 
81
    }
 
82
    
 
83
    public void setWebSaveOnly(boolean b) {
 
84
        isWebSaveOnly = b;
 
85
        if (b) {
 
86
            setRGB(getRGB());
 
87
        }
 
88
        fireColorChanged(-1);
 
89
    }
 
90
    public boolean isWebSaveOnly() {
 
91
        return isWebSaveOnly;
 
92
    }
 
93
    
 
94
    public static boolean isWebSave(int rgb) {
 
95
        return (rgb & 0xffffff) == (toWebSave(rgb) & 0xffffff);
 
96
    }
 
97
    public static int toWebSave(int rgb) {
 
98
        return
 
99
        (rgb & 0xff000000)
 
100
        | ((Math.round(((rgb & 0xff0000) >> 16) / 51f) * 51) << 16)
 
101
        | ((Math.round(((rgb & 0x00ff00) >> 8) / 51f) * 51) << 8)
 
102
        | (Math.round((rgb & 0x0000ff) / 51f) * 51);
 
103
    }
 
104
}