BUGFIX: 获取pantone值为空时返回rgb和hsv

This commit is contained in:
2025-03-18 18:47:05 +08:00
parent 654a3829c6
commit 0500557dca
3 changed files with 93 additions and 8 deletions

View File

@@ -28,4 +28,70 @@ public class PantoneUtils {
return new int[]{Math.round(hsv[0]), Math.round(hsv[1] * 100), Math.round(hsv[2] * 100)};
}
public static int[] hsvToRgb(int h, int s, int v) {
// 确保 h 在 [0, 360) 范围内
h = h % 360;
if (h < 0) {
h += 360;
}
// 确保 s 和 v 在 [0, 100] 范围内
s = Math.max(0, Math.min(100, s));
v = Math.max(0, Math.min(100, v));
// 将 s 和 v 映射到 [0, 1] 范围
float sNorm = s / 100.0f;
float vNorm = v / 100.0f;
// 计算色相所在的区间
int hi = (h / 60) % 6;
float f = (h / 60.0f) - hi;
float p = vNorm * (1 - sNorm);
float q = vNorm * (1 - f * sNorm);
float t = vNorm * (1 - (1 - f) * sNorm);
float r, g, b;
switch (hi) {
case 0:
r = vNorm;
g = t;
b = p;
break;
case 1:
r = q;
g = vNorm;
b = p;
break;
case 2:
r = p;
g = vNorm;
b = t;
break;
case 3:
r = p;
g = q;
b = vNorm;
break;
case 4:
r = t;
g = p;
b = vNorm;
break;
case 5:
r = vNorm;
g = p;
b = q;
break;
default:
throw new RuntimeException("Invalid HSV values");
}
// 将 RGB 值从 [0, 1] 转换为 [0, 255]
int red = Math.round(r * 255);
int green = Math.round(g * 255);
int blue = Math.round(b * 255);
return new int[]{red, green, blue};
}
}

View File

@@ -5,7 +5,6 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
@NoArgsConstructor
@Data
@@ -43,4 +42,15 @@ public class PantoneVO {
this.name = name;
this.tcx = tcx;
}
public PantoneVO(Integer r, Integer g, Integer b, Integer h, Integer s, Integer v) {
this.r = r;
this.g = g;
this.b = b;
this.h = h;
this.s = s;
this.v = v;
this.name = "--";
this.tcx = "--";
}
}

View File

@@ -45,7 +45,7 @@ public class PanToneServiceImpl extends ServiceImpl<PanToneMapper, PanTone> impl
queryWrapper.eq("pantone_index", colorLookupTable.getColorIndex());
PanTone panTone = panToneMapper.selectOne(queryWrapper);
if (Objects.isNull(panTone)) {
throw new BusinessException("pantone.not.found");
return null;
}
return coverPanToneToVo(panTone);
}
@@ -176,16 +176,19 @@ public class PanToneServiceImpl extends ServiceImpl<PanToneMapper, PanTone> impl
@Override
public PantoneVO getPantoneByRgb(String color) {
GetRgbByHsvBatchDTO getRgbByHsvBatchDTO = new GetRgbByHsvBatchDTO();
int[] rgb = Arrays.stream(color.split("\\s+")).mapToInt(Integer::parseInt).toArray();
int[] hsv = PantoneUtils.rgbToHsv(rgb);
if (!StringUtil.isNullOrEmpty(color)) {
int[] rgb = Arrays.stream(color.split("\\s+")).mapToInt(Integer::parseInt).toArray();
int[] hsv = PantoneUtils.rgbToHsv(rgb);
getRgbByHsvBatchDTO.setH(hsv[0]);
getRgbByHsvBatchDTO.setS(hsv[1]);
getRgbByHsvBatchDTO.setV(hsv[2]);
}
return getByHSV(getRgbByHsvBatchDTO.getH(), getRgbByHsvBatchDTO.getS(), getRgbByHsvBatchDTO.getV());
PantoneVO pantoneVO = getByHSV(getRgbByHsvBatchDTO.getH(), getRgbByHsvBatchDTO.getS(), getRgbByHsvBatchDTO.getV());
if (Objects.isNull(pantoneVO)) {
pantoneVO = new PantoneVO(rgb[0], rgb[1], rgb[2], hsv[0], hsv[1], hsv[2]);
}
return pantoneVO;
}
@Override
@@ -215,7 +218,12 @@ public class PanToneServiceImpl extends ServiceImpl<PanToneMapper, PanTone> impl
QueryWrapper<PanTone> queryWrapper = new QueryWrapper<>();
queryWrapper.in("pantone_index", colorLookupTables.stream()
.map(ColorLookupTable::getColorIndex).collect(Collectors.toList()));
return coverPanToneToVoList(panToneMapper.selectList(queryWrapper), IndexToValue, valueToHsv, hsvBatch);
List<PantoneVO> pantoneVOS = coverPanToneToVoList(panToneMapper.selectList(queryWrapper), IndexToValue, valueToHsv, hsvBatch);
if (pantoneVOS.isEmpty() && hsvBatch.size() == 1){
int[] rgb = PantoneUtils.hsvToRgb(hsvBatch.get(0).getH(), hsvBatch.get(0).getS(), hsvBatch.get(0).getV());
pantoneVOS.add(new PantoneVO(rgb[0], rgb[1], rgb[2], hsvBatch.get(0).getH(), hsvBatch.get(0).getS(), hsvBatch.get(0).getV()));
}
return pantoneVOS;
}
private PantoneVO coverPanToneToVo(PanTone panTone) {
@@ -234,7 +242,8 @@ public class PanToneServiceImpl extends ServiceImpl<PanToneMapper, PanTone> impl
, Map<Integer, Integer> indexToValue, Map<Integer, GetRgbByHsvBatchDTO> valueToHsv,
List<GetRgbByHsvBatchDTO> hsvBatch) {
if (CollectionUtil.isEmpty(panTones)) {
throw new BusinessException("panTones.not.found", ResultEnum.PROMPT.getCode());
return Lists.newArrayList();
// throw new BusinessException("panTones.not.found", ResultEnum.PROMPT.getCode());
}
List<PantoneVO> templateResposne = CopyUtil.copyList(panTones, PantoneVO.class, (o, d) -> {
d.setId(o.getPantoneIndex());