Java Color Value getBlueGreyColor(Color base)

Here you can find the source of getBlueGreyColor(Color base)

Description

get Blue Grey Color

License

Open Source License

Declaration

public static Color getBlueGreyColor(Color base) 

Method Source Code

//package com.java2s;
/*//from w  ww  . j av  a  2 s .  co m
 *  NimbusHelper.java
 *  (SwingOSC)
 *
 *  Copyright (c) 2005-2012 Hanns Holger Rutz. All rights reserved.
 *
 *   This software is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either
 *   version 2, june 1991 of the License, or (at your option) any later version.
 *
 *   This software is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *   General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public
 *   License (gpl.txt) along with this software; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *   For further information, please contact Hanns Holger Rutz at
 *   contact@sciss.de
 */

import javax.swing.UIDefaults;

import java.awt.Color;

public class Main {
    private static UIDefaults nimbusDefaults;
    private static final float[] hsbArr = new float[3];

    public static Color getBlueGreyColor(Color base) {
        final Color c = nimbusDefaults == null ? null : nimbusDefaults.getColor("nimbusBlueGrey");
        return c == null ? defaultBlueGreyColor(base) : c;
    }

    private static Color defaultBlueGreyColor(Color base) {
        return adjustColor(base, 0.032459438f, -0.52518797f, 0.19607842f, 0);
    }

    public static Color adjustColor(Color c, float hueOffset, float satOffset, float briOffset, int alphaOffset) {
        final boolean sameColor = hueOffset == 0f && satOffset == 0f && briOffset == 0f;
        final boolean sameAlpha = alphaOffset == 0;
        if (sameColor) {
            if (sameAlpha)
                return c;
            // don't know what's going on here. nimbus defaults ColorUIResources have alpha values of zero sometimes
            final int cAlpha = /* c.getTransparency() == Transparency.TRANSLUCENT ? */ c.getAlpha() /* : 0xFF */;
            return new Color(c.getRed(), c.getGreen(), c.getBlue(),
                    Math.max(0, Math.min(0xFF, cAlpha + alphaOffset)));
        }

        Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), hsbArr);
        final float hue = hsbArr[0] + hueOffset;
        final float sat = Math.max(0f, Math.min(1f, hsbArr[1] + satOffset));
        final float bri = Math.max(0f, Math.min(1f, hsbArr[2] + briOffset));
        final int rgb = Color.HSBtoRGB(hue, sat, bri);
        // (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue).
        //        final int r = (rgb >> 16) & 0xFF;
        //        final int g = (rgb >> 8) & 0xFF;
        //        final int b = rgb & 0xFF;
        final int cAlpha = /* c.getTransparency() == Transparency.TRANSLUCENT ? */ c.getAlpha() /* : 0xFF */;
        final int a = sameAlpha ? cAlpha : Math.max(0, Math.min(0xFF, cAlpha + alphaOffset));
        final int rgba = (rgb & 0xFFFFFF) | (a << 24);
        return new Color(rgba, true);
    }
}

Related

  1. getAsNotUIResource(Color color)
  2. getBackgroundColor()
  3. getBorder(Color clr, int thickness)
  4. getColorBackground()
  5. getColorChooser()
  6. getColorForeground()