001/*
002Copyright 2006 Jerry Huxtable
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008   http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015*/
016
017package com.jhlabs.image;
018
019import java.awt.*;
020import java.awt.image.*;
021
022public class HSBAdjustFilter extends PointFilter {
023        
024        public float hFactor, sFactor, bFactor;
025        private float[] hsb = new float[3];
026        
027        public HSBAdjustFilter() {
028                this(0, 0, 0);
029        }
030
031        public HSBAdjustFilter(float r, float g, float b) {
032                hFactor = r;
033                sFactor = g;
034                bFactor = b;
035                canFilterIndexColorModel = true;
036        }
037
038        public void setHFactor( float hFactor ) {
039                this.hFactor = hFactor;
040        }
041        
042        public float getHFactor() {
043                return hFactor;
044        }
045        
046        public void setSFactor( float sFactor ) {
047                this.sFactor = sFactor;
048        }
049        
050        public float getSFactor() {
051                return sFactor;
052        }
053        
054        public void setBFactor( float bFactor ) {
055                this.bFactor = bFactor;
056        }
057        
058        public float getBFactor() {
059                return bFactor;
060        }
061        
062        public int filterRGB(int x, int y, int rgb) {
063                int a = rgb & 0xff000000;
064                int r = (rgb >> 16) & 0xff;
065                int g = (rgb >> 8) & 0xff;
066                int b = rgb & 0xff;
067                Color.RGBtoHSB(r, g, b, hsb);
068                hsb[0] += hFactor;
069                while (hsb[0] < 0)
070                        hsb[0] += Math.PI*2;
071                hsb[1] += sFactor;
072                if (hsb[1] < 0)
073                        hsb[1] = 0;
074                else if (hsb[1] > 1.0)
075                        hsb[1] = 1.0f;
076                hsb[2] += bFactor;
077                if (hsb[2] < 0)
078                        hsb[2] = 0;
079                else if (hsb[2] > 1.0)
080                        hsb[2] = 1.0f;
081                rgb = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
082                return a | (rgb & 0xffffff);
083        }
084
085        public String toString() {
086                return "Colors/Adjust HSB...";
087        }
088}
089