package cn.jrymos.core.identifier;
import cn.jrymos.core.domain.MyColor;
import cn.jrymos.core.enums.BrightnessEnum;
import cn.jrymos.core.enums.MyColorEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.awt.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
@RequiredArgsConstructor
public class ColorIdentifierService implements ColorIdentifier {
private final BrightnessColorFactor brightnessColorFactor;
@Override
public MyColor identify(Color color) {
MyColorEnum myColorEnum = getMyColorEnum(color);
BrightnessEnum brightnessEnum = BrightnessEnum.getByColor(color);
return new MyColor().setColorEnum(myColorEnum).setBrightnessEnum(brightnessEnum).setColor(color);
}
private MyColorEnum getMyColorEnum(Color color) {
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
List<Integer> ns = Stream.of(red, green, blue).sorted().collect(Collectors.toList());
int maxN = ns.get(2);
int midN = ns.get(1);
int minN = ns.get(0);
//1. 是否是灰色: 三色较为接近,最大差值小于10,灰色
int d3 = maxN - minN;
if (d3 < 10) {
return MyColorEnum.GREY;
}
//2. 红绿蓝:最大色-第二大色>第二色-第三色
int d1 = maxN - midN;
int d2 = midN - minN;
if (d1 * brightnessColorFactor.getBaseFactor(red, green, blue) > d2) {
return maxN == red ? MyColorEnum.RED : maxN == green ? MyColorEnum.GREEN : MyColorEnum.BLUE;
}
//3. 黄青紫:黄色缺蓝,青色缺红,紫色缺绿;第二色-第三色 > 系数 * (第一色-第二色)
if (d2 > brightnessColorFactor.getMixFactor(red, green, blue) * d1) {
return minN == red ? MyColorEnum.CYAN : minN == green ? MyColorEnum.PURPLE : MyColorEnum.YELLOW;
}
//4. 青绿、黄绿、橙色、紫红、紫蓝、青蓝:取前两色,红色+绿色=橙色,绿色+红色=黄绿,红色+蓝色=紫红,蓝色+红色=紫蓝,蓝色+绿色=绿蓝,绿色+蓝色=青绿
if (maxN == red) {
return midN == green ? MyColorEnum.ORANGE : MyColorEnum.PINK;
}
if (maxN == green) {
return midN == red ? MyColorEnum.YELLOW_GREEN : MyColorEnum.CYAN_GREEN;
}
return midN == red ? MyColorEnum.PURPLE_BLUE : MyColorEnum.CYAN_BLUE;
}
public static void main(String[] args) {
ColorIdentifierService service = new ColorIdentifierService(new BrightnessColorFactorImpl());
lightCheck(service);
darkCheck(service);
}
private static void lightCheck(ColorIdentifierService service) {
System.out.println(service.getMyColorEnum(new Color(255, 100, 180)) + "\t" + MyColorEnum.RED);
System.out.println(service.getMyColorEnum(new Color(255, 185, 100)) + "\t" + MyColorEnum.ORANGE);
System.out.println(service.getMyColorEnum(new Color(255, 230, 100)) + "\t" + MyColorEnum.YELLOW);
System.out.println(service.getMyColorEnum(new Color(245, 255, 100)) + "\t" + MyColorEnum.YELLOW_GREEN);
System.out.println(service.getMyColorEnum(new Color(185, 255, 100)) + "\t" + MyColorEnum.GREEN);
System.out.println(service.getMyColorEnum(new Color(100, 255, 185)) + "\t" + MyColorEnum.CYAN_GREEN);
System.out.println(service.getMyColorEnum(new Color(100, 255, 225)) + "\t" + MyColorEnum.CYAN);
System.out.println(service.getMyColorEnum(new Color(100, 220, 255)) + "\t" + MyColorEnum.CYAN_BLUE);
System.out.println(service.getMyColorEnum(new Color(100, 175, 255)) + "\t" + MyColorEnum.BLUE);
System.out.println(service.getMyColorEnum(new Color(165, 100, 255)) + "\t" + MyColorEnum.PURPLE_BLUE);
System.out.println(service.getMyColorEnum(new Color(195, 100, 255)) + "\t" + MyColorEnum.PURPLE);
System.out.println(service.getMyColorEnum(new Color(255, 100, 220)) + "\t" + MyColorEnum.PINK);
}
private static void darkCheck(ColorIdentifierService service) {
System.out.println(service.getMyColorEnum(new Color(128, 0, 65)) + "\t" + MyColorEnum.RED);
System.out.println(service.getMyColorEnum(new Color(128, 70, 0)) + "\t" + MyColorEnum.ORANGE);
System.out.println(service.getMyColorEnum(new Color(128, 110, 0)) + "\t" + MyColorEnum.YELLOW);
System.out.println(service.getMyColorEnum(new Color(118, 128, 0)) + "\t" + MyColorEnum.YELLOW_GREEN);
System.out.println(service.getMyColorEnum(new Color(75, 128, 0)) + "\t" + MyColorEnum.GREEN);
System.out.println(service.getMyColorEnum(new Color(0, 128, 75)) + "\t" + MyColorEnum.CYAN_GREEN);
System.out.println(service.getMyColorEnum(new Color(0, 128, 105)) + "\t" + MyColorEnum.CYAN);
System.out.println(service.getMyColorEnum(new Color(0, 105, 128)) + "\t" + MyColorEnum.CYAN_BLUE);
System.out.println(service.getMyColorEnum(new Color(0, 75, 128)) + "\t" + MyColorEnum.BLUE);
System.out.println(service.getMyColorEnum(new Color(50, 0, 128)) + "\t" + MyColorEnum.PURPLE_BLUE);
System.out.println(service.getMyColorEnum(new Color(70, 0, 128)) + "\t" + MyColorEnum.PURPLE);
System.out.println(service.getMyColorEnum(new Color(128, 0, 100)) + "\t" + MyColorEnum.PINK);
}
}