BUGFIX: 获取RGB对应的PanTone数据失败,导致返回颜色为空

This commit is contained in:
2025-03-18 17:39:39 +08:00
parent fccafba438
commit 654a3829c6

View File

@@ -73,8 +73,7 @@ public class PanToneServiceImpl extends ServiceImpl<PanToneMapper, PanTone> impl
@Override
public Map<String, PantoneVO> getPantoneByRgbBatch(List<String> colors) {
HashMap<Integer, String> colorValueRgb = new HashMap<>();
/*HashMap<Integer, String> colorValueRgb = new HashMap<>();
HashMap<Integer, String> colorIndexRgb = new HashMap<>();
ArrayList<Integer> values = new ArrayList<>();
colors.forEach(color -> {
@@ -91,12 +90,11 @@ public class PanToneServiceImpl extends ServiceImpl<PanToneMapper, PanTone> impl
colorIndexRgb.put(colorValue.getColorIndex(), colorValueRgb.get(colorValue.getColorValue()));
});
List<PanTone> panTones = panToneService.listByIds(colorIndexRgb.keySet());
List<PanTone> panTones = panToneService.listByIds(colorIndexRgb.keySet()); // pantone数据不完整导致有的index无对应pantone值
ArrayList<PantoneVO> pantoneVOS = new ArrayList<>();
panTones.forEach(panTone -> {
pantoneVOS.add(coverPanToneToVo(panTone));
});
HashMap<String, PantoneVO> colorPantoneVO = new HashMap<>();
pantoneVOS.forEach(pantoneVO -> {
int colorIndex = pantoneVO.getId();
@@ -109,7 +107,6 @@ public class PanToneServiceImpl extends ServiceImpl<PanToneMapper, PanTone> impl
colorPantoneVO.put(colorIndexRgb.get(pantoneVO.getId()), pantoneVO);
}
});
// 覆盖pantone中的rgb值,使用前端传过来的rgb值
for (Map.Entry<String, PantoneVO> pantoneVOEntry : colorPantoneVO.entrySet()) {
int[] rgb = Arrays.stream(pantoneVOEntry.getKey().split("\\s+")).mapToInt(Integer::parseInt).toArray();
@@ -117,6 +114,60 @@ public class PanToneServiceImpl extends ServiceImpl<PanToneMapper, PanTone> impl
pantoneVOEntry.getValue().setG(rgb[1]);
pantoneVOEntry.getValue().setB(rgb[2]);
}
return colorPantoneVO;*/
// 用于存储 RGB 值对应的 HSV 计算值
Map<Integer, String> colorValueRgb = new HashMap<>();
// 用于存储 ColorIndex 对应的 RGB 值
Map<Integer, String> colorIndexRgb = new HashMap<>();
// 存储所有计算出的 HSV 值
List<Integer> values = new ArrayList<>();
// 1. 遍历 colors计算 HSV 值并存储
colors.forEach(color -> {
int[] rgb = Arrays.stream(color.split("\\s+")).mapToInt(Integer::parseInt).toArray();
int[] hsv = PantoneUtils.rgbToHsv(rgb);
int value = (hsv[0] * 101 * 101) + (hsv[1] * 101) + hsv[2];
colorValueRgb.put(value, color);
values.add(value);
});
// 2. 查询 ColorLookupTable 数据
List<ColorLookupTable> colorValueList = colorLoopUpTableService.getByColorValueList(values);
colorValueList.forEach(colorValue -> {
colorIndexRgb.put(colorValue.getColorIndex(), colorValueRgb.get(colorValue.getColorValue()));
});
// 3. 查询 Pantone 数据
Set<Integer> colorIndexes = colorIndexRgb.keySet();
List<PanTone> panTones = panToneService.listByIds(colorIndexes);
// 4. 将 PanTone 转换为 PantoneVO并处理缺失的 Pantone 数据
Map<Integer, PantoneVO> pantoneVOMap = new HashMap<>();
panTones.forEach(panTone -> {
pantoneVOMap.put(panTone.getPantoneIndex(), coverPanToneToVo(panTone));
});
// 5. 处理缺失的 Pantone 数据,创建默认的 PantoneVO
for (Integer colorIndex : colorIndexes) {
if (!pantoneVOMap.containsKey(colorIndex)) {
String rgbValue = colorIndexRgb.get(colorIndex);
int[] rgb = Arrays.stream(rgbValue.split("\\s+")).mapToInt(Integer::parseInt).toArray();
PantoneVO defaultPantoneVO = new PantoneVO();
defaultPantoneVO.setR(rgb[0]);
defaultPantoneVO.setG(rgb[1]);
defaultPantoneVO.setB(rgb[2]);
pantoneVOMap.put(colorIndex, defaultPantoneVO);
}
}
// 6. 构建最终结果
Map<String, PantoneVO> colorPantoneVO = new HashMap<>();
for (Map.Entry<Integer, PantoneVO> entry : pantoneVOMap.entrySet()) {
Integer colorIndex = entry.getKey();
PantoneVO pantoneVO = entry.getValue();
String rgbValue = colorIndexRgb.get(colorIndex);
colorPantoneVO.put(rgbValue, pantoneVO);
}
return colorPantoneVO;
}