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.*; 021import java.util.*; 022 023/** 024 * A filter which draws a gradient interpolated between four colors defined at the corners of the image. 025 */ 026public class FourColorFilter extends PointFilter { 027 028 private int width; 029 private int height; 030 private int colorNW; 031 private int colorNE; 032 private int colorSW; 033 private int colorSE; 034 private int rNW, gNW, bNW; 035 private int rNE, gNE, bNE; 036 private int rSW, gSW, bSW; 037 private int rSE, gSE, bSE; 038 039 public FourColorFilter() { 040 setColorNW( 0xffff0000 ); 041 setColorNE( 0xffff00ff ); 042 setColorSW( 0xff0000ff ); 043 setColorSE( 0xff00ffff ); 044 } 045 046 public void setColorNW( int color ) { 047 this.colorNW = color; 048 rNW = (color >> 16) & 0xff; 049 gNW = (color >> 8) & 0xff; 050 bNW = color & 0xff; 051 } 052 053 public int getColorNW() { 054 return colorNW; 055 } 056 057 public void setColorNE( int color ) { 058 this.colorNE = color; 059 rNE = (color >> 16) & 0xff; 060 gNE = (color >> 8) & 0xff; 061 bNE = color & 0xff; 062 } 063 064 public int getColorNE() { 065 return colorNE; 066 } 067 068 public void setColorSW( int color ) { 069 this.colorSW = color; 070 rSW = (color >> 16) & 0xff; 071 gSW = (color >> 8) & 0xff; 072 bSW = color & 0xff; 073 } 074 075 public int getColorSW() { 076 return colorSW; 077 } 078 079 public void setColorSE( int color ) { 080 this.colorSE = color; 081 rSE = (color >> 16) & 0xff; 082 gSE = (color >> 8) & 0xff; 083 bSE = color & 0xff; 084 } 085 086 public int getColorSE() { 087 return colorSE; 088 } 089 090 public void setDimensions(int width, int height) { 091 this.width = width; 092 this.height = height; 093 super.setDimensions(width, height); 094 } 095 096 public int filterRGB(int x, int y, int rgb) { 097 float fx = (float)x / width; 098 float fy = (float)y / height; 099 float p, q; 100 101 p = rNW + (rNE - rNW) * fx; 102 q = rSW + (rSE - rSW) * fx; 103 int r = (int)( p + (q - p) * fy + 0.5f ); 104 105 p = gNW + (gNE - gNW) * fx; 106 q = gSW + (gSE - gSW) * fx; 107 int g = (int)( p + (q - p) * fy + 0.5f ); 108 109 p = bNW + (bNE - bNW) * fx; 110 q = bSW + (bSE - bSW) * fx; 111 int b = (int)( p + (q - p) * fy + 0.5f ); 112 113 return 0xff000000 | (r << 16) | (g << 8) | b; 114 } 115 116 public String toString() { 117 return "Texture/Four Color Fill..."; 118 } 119}