📄 fscolortest.java
字号:
/*
* FSColorTest.java
* Transform
*
* Copyright (c) 2001-2006 Flagstone Software Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Flagstone Software Ltd. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.flagstone.transform.test;
import com.flagstone.transform.*;
public class FSColorTest extends FSClassTest
{
private static int[][] opaque = new int[][] {
{ 0, 0, 0},
{ 51, 0, 0},
{ 51, 102, 0},
{ 0, 102, 0},
{ 0, 102, 153},
{ 0, 0, 153},
{ 51, 0, 153},
{ 51, 102, 153},
};
private static int[][] alpha = new int[][] {
{ 51, 102, 153, 0},
{ 51, 102, 153, 204},
{ 51, 0, 153, 204},
{ 51, 0, 153, 0},
{ 0, 0, 153, 0},
{ 0, 0, 153, 204},
{ 0, 102, 153, 204},
{ 0, 102, 153, 0},
{ 0, 102, 0, 0},
{ 0, 102, 0, 204},
{ 51, 102, 0, 204},
{ 51, 102, 0, 0},
{ 51, 0, 0, 0},
{ 51, 0, 0, 204},
{ 0, 0, 0, 204},
{ 0, 0, 0, 0},
};
private static FSColor[] colours = null;
private static FSColor[] coloursWithAlpha = null;
public static FSColor[] colours()
{
if (colours == null)
{
colours = new FSColor[opaque.length];
for (int i=0; i<opaque.length; i++) {
colours[i] = new FSColor(opaque[i][0], opaque[i][1], opaque[i][2]);
}
}
return colours;
}
public static int[][] encodedColours()
{
int[][] data = new int[opaque.length][];
for (int i=0; i<opaque.length; i++)
{
data[i] = new int[opaque[i].length];
for (int j=0; j<opaque[i].length; j++) {
data[i][j] = opaque[i][j];
}
}
return data;
}
public static FSColor[] coloursWithAlpha()
{
if (coloursWithAlpha == null)
{
coloursWithAlpha = new FSColor[alpha.length];
for (int i=0; i<alpha.length; i++)
{
coloursWithAlpha[i] = new FSColor(alpha[i][0], alpha[i][1], alpha[i][2], alpha[i][3]);
}
}
return colours;
}
/**
* @testng.test groups="color, constructors"
*/
public void checkConstructors()
{
FSColor colour = null;
for (int i=0; i<opaque.length; i++)
{
colour = new FSColor(opaque[i][0], opaque[i][1], opaque[i][2]);
checkColours(colour, opaque[i]);
}
}
/**
* @testng.test groups="color, constructors"
*/
public void checkConstructorsWithAlpha()
{
FSColor colour = null;
for (int i=0; i<alpha.length; i++)
{
colour = new FSColor(alpha[i][0], alpha[i][1], alpha[i][2], alpha[i][3]);
checkColours(colour, alpha[i]);
}
}
/**
* @testng.test groups="color, constructors"
*/
public void checkCopyConstructors()
{
for (int i=0; i<opaque.length; i++)
{
FSColor colour = new FSColor(opaque[i][0], opaque[i][1], opaque[i][2]);
checkColours(new FSColor(colour), opaque[i]);
}
}
/**
* @testng.test groups="color, constructors"
*/
public void checkCopyConstructorsWithAlpha()
{
for (int i=0; i<alpha.length; i++)
{
FSColor colour = new FSColor(alpha[i][0], alpha[i][1], alpha[i][2], alpha[i][3]);
checkColours(new FSColor(colour), alpha[i]);
}
}
/**
* @testng.test groups="color, accessors"
*/
public void checkAccessors()
{
FSColor colour = new FSColor(-1, -1, -1);
for (int i=0; i<opaque.length; i++)
{
colour.setRed(opaque[i][0]);
colour.setGreen(opaque[i][1]);
colour.setBlue(opaque[i][2]);
checkColours(colour, opaque[i]);
}
colour = new FSColor(-1, -1, -1);
for (int i=0; i<opaque.length; i++)
{
colour.setChannels(opaque[i][0], opaque[i][1], opaque[i][2]);
checkColours(colour, opaque[i]);
}
}
/**
* @testng.test groups="color, accessors"
*/
public void checkAccessorsWithAlpha()
{
FSColor colour = new FSColor(-1, -1, -1, -1);
for (int i=0; i<alpha.length; i++)
{
colour.setRed(alpha[i][0]);
colour.setGreen(alpha[i][1]);
colour.setBlue(alpha[i][2]);
colour.setAlpha(alpha[i][3]);
checkColours(colour, alpha[i]);
}
colour = new FSColor(-1, -1, -1, -1);
for (int i=0; i<alpha.length; i++)
{
colour.setChannels(alpha[i][0], alpha[i][1], alpha[i][2], alpha[i][3]);
checkColours(colour, alpha[i]);
}
}
/**
* @testng.test groups="color, utilities"
*/
public void checkEquals()
{
for (int i=0; i<opaque.length; i++)
{
int next = (i+1)%opaque.length;
FSColor a = new FSColor(opaque[i][0], opaque[i][1], opaque[i][2]);
FSColor b = new FSColor(opaque[i][0], opaque[i][1], opaque[i][2]);
FSColor c = new FSColor(opaque[next][0], opaque[next][1], opaque[next][2]);
FSColor d = new FSColor(opaque[i][0], opaque[i][1], opaque[i][2], 255);
FSColor e = new FSColor(opaque[i][0], opaque[i][1], opaque[i][2], 128);
checkEqual(a, b);
checkNotEqual(a, c);
checkEqual(a, d);
checkNotEqual(a, e);
}
}
/**
* @testng.test groups="color, encode"
*/
public void checkEncode()
{
FSCoder coder = new FSCoder(FSCoder.LITTLE_ENDIAN, 0);
for (int i=0; i<opaque.length; i++)
{
FSColor colour = new FSColor(opaque[i][0], opaque[i][1], opaque[i][2]);
checkEncode(coder, colour, opaque[i]);
checkColours(colour, opaque[i]);
}
}
/**
* @testng.test groups="color, encode"
*/
public void checkEncodeWithAlpha()
{
FSCoder coder = new FSCoder(FSCoder.LITTLE_ENDIAN, 0);
coder.setContext(FSCoder.TransparentColors, 1);
for (int i=0; i<alpha.length; i++)
{
FSColor colour = new FSColor(alpha[i][0], alpha[i][1], alpha[i][2], alpha[i][3]);
checkEncode(coder, colour, alpha[i]);
checkColours(colour, alpha[i]);
}
}
/**
* @testng.test groups="color, decode"
*/
public void checkDecode()
{
for (int i=0; i<opaque.length; i++)
{
FSCoder coder = new FSCoder(FSCoder.LITTLE_ENDIAN, compact(opaque[i]));
checkColours(new FSColor(coder), opaque[i]);
checkDecode(coder, opaque[i].length);
}
}
/**
* @testng.test groups="color, decode"
*/
public void checkDecodeWithAlpha()
{
for (int i=0; i<opaque.length; i++)
{
FSCoder coder = new FSCoder(FSCoder.LITTLE_ENDIAN, compact(alpha[i]));
coder.setContext(FSCoder.TransparentColors, 1);
checkColours(new FSColor(coder), alpha[i]);
checkDecode(coder, alpha[i].length);
}
}
private void checkColours(FSColor colour, int[] expected)
{
assert(colour.getRed() == expected[0]) : "Red channel does not match expected value";
assert(colour.getGreen() == expected[1]) : "Green channel does not match expected value";
assert(colour.getBlue() == expected[2]) : "Blue channel does not match expected value";
if (expected.length == 4)
assert(colour.getAlpha() == expected[3]) : "Alpha channel does not match expected value";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -