Skip to content

Commit

Permalink
integrated SvenWoltmann/color-thief-java
Browse files Browse the repository at this point in the history
  • Loading branch information
melistik committed Jul 27, 2018
1 parent f05a1a5 commit 48753c9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public class AssetMeta implements Serializable {
*/
private Resolution resolution;

/**
* only filled in case of image asset
*/
private ColorPalette colorPalette;

/**
* only filled in case of batch downloaded image
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.rocketbase.commons.dto.asset;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ColorPalette implements Serializable {

/**
* dominant color
*/
private String primary;

/**
* other colors ordered by priority (not containing primary)
*/
private List<String> colors;

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static int[][] unpackRGBArray(int[] packedRgbArray) {
*
* @return the string representation
*/
private static String createRGBString(int[] rgb) {
public static String createRGBString(int[] rgb) {
return "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")";
}

Expand All @@ -94,7 +94,7 @@ private static String createRGBString(int[] rgb) {
*
* @return the HTML hex color code
*/
private static String createRGBHexString(int[] rgb) {
public static String createRGBHexString(int[] rgb) {
String rgbHex = Integer.toHexString(rgb[0] << 16 | rgb[1] << 8 | rgb[2]);

// Left-pad with 0s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public AssetRead fromEntityWithoutPreviews(AssetEntity entity) {
.fileSize(entity.getFileSize())
.originalFilename(entity.getOriginalFilename())
.resolution(entity.getResolution())
.colorPalette(entity.getColorPalette())
.referenceUrl(entity.getReferenceUrl())
.build())
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.rocketbase.commons.model;

import io.rocketbase.commons.dto.asset.AssetMeta;
import io.rocketbase.commons.dto.asset.AssetReference;
import io.rocketbase.commons.dto.asset.AssetType;
import io.rocketbase.commons.dto.asset.Resolution;
import io.rocketbase.commons.dto.asset.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -49,6 +46,11 @@ public class AssetEntity {
*/
private Resolution resolution;

/**
* only filled in case of image asset
*/
private ColorPalette colorPalette;

/**
* only filled in case of batch downloaded image
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.rocketbase.commons.service;

import com.google.common.base.Stopwatch;
import de.androidpit.colorthief.ColorThief;
import de.androidpit.colorthief.MMCQ;
import de.androidpit.colorthief.RGBUtil;
import io.rocketbase.commons.dto.asset.AssetType;
import io.rocketbase.commons.dto.asset.ColorPalette;
import io.rocketbase.commons.dto.asset.Resolution;
import io.rocketbase.commons.exception.InvalidContentTypeException;
import io.rocketbase.commons.exception.NotAllowedAssetTypeException;
Expand All @@ -23,7 +27,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
Expand Down Expand Up @@ -120,6 +126,14 @@ private AssetEntity saveAndUploadAsset(AssetType type, File file, String origina
} else {
log.trace("file not readable");
}
MMCQ.CMap colorMap = ColorThief.getColorMap(bufferedImage, 4);
if (colorMap != null && !colorMap.vboxes.isEmpty()) {
List<String> colors = colorMap.vboxes.stream()
.map(v -> RGBUtil.createRGBHexString(v.avg(false)))
.collect(Collectors.toList());
entity.setColorPalette(new ColorPalette(colors.get(0), colors.size() > 1 ? colors.subList(1, colors.size() - 1) : null));
}

} catch (Exception e) {
log.error("could not read file information from entity {}", entity);
}
Expand Down

0 comments on commit 48753c9

Please sign in to comment.