From ddd907e9e69571f7b68b5cecdfabee92c6dfa61e Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 9 Mar 2017 02:19:27 +0100 Subject: [PATCH] Solve issue with CMYK and CMYK255 --- .../colormode/mode/CMYK.java | 36 +++++++++--- .../colormode/mode/CMYK255.java | 57 +------------------ .../fragment/ChromaColorFragment.java | 5 +- .../androidclearchroma/view/ChannelView.java | 3 +- .../sample/MainActivity.java | 5 ++ 5 files changed, 38 insertions(+), 68 deletions(-) diff --git a/library/src/main/java/com/kunzisoft/androidclearchroma/colormode/mode/CMYK.java b/library/src/main/java/com/kunzisoft/androidclearchroma/colormode/mode/CMYK.java index 76ba0cf..dbcd633 100644 --- a/library/src/main/java/com/kunzisoft/androidclearchroma/colormode/mode/CMYK.java +++ b/library/src/main/java/com/kunzisoft/androidclearchroma/colormode/mode/CMYK.java @@ -13,41 +13,59 @@ */ public class CMYK implements AbstractColorMode { + protected int MAX_VALUE; + + public CMYK() { + MAX_VALUE = 100; + } + + private double fraction() { + return 255 / (double) MAX_VALUE; + } + @Override public List getChannels() { List list = new ArrayList<>(); - list.add(new Channel(R.string.channel_cyan, 0, 100, new Channel.ColorExtractor() { + list.add(new Channel(R.string.channel_cyan, 0, MAX_VALUE, new Channel.ColorExtractor() { @Override public int extract(int color) { - return 100 - (int) (Color.red((int) (color * 2.55)) / 2.55); + // C = (1-R'-K) / (1-K) + return (int) (MAX_VALUE * ((MAX_VALUE - (Color.red(color)/fraction()) - black(color) ) / (MAX_VALUE - black(color)))); } })); - list.add(new Channel(R.string.channel_magenta, 0, 100, new Channel.ColorExtractor() { + list.add(new Channel(R.string.channel_magenta, 0, MAX_VALUE, new Channel.ColorExtractor() { @Override public int extract(int color) { - return 100 - (int) (Color.green((int) (color * 2.55)) / 2.55); + // M = (1-G'-K) / (1-K) + return (int) (MAX_VALUE * ((MAX_VALUE - (Color.green(color)/fraction()) - black(color) ) / (MAX_VALUE - black(color)))); } })); - list.add(new Channel(R.string.channel_yellow, 0, 100, new Channel.ColorExtractor() { + list.add(new Channel(R.string.channel_yellow, 0, MAX_VALUE, new Channel.ColorExtractor() { @Override public int extract(int color) { - return 100 - (int) (Color.blue((int) (color * 2.55)) / 2.55); + // M = (1-Y'-K) / (1-K) + return (int) (MAX_VALUE * ((MAX_VALUE - (Color.blue(color)/fraction()) - black(color) ) / (MAX_VALUE - black(color)))); } })); - list.add(new Channel(R.string.channel_black, 0, 100, new Channel.ColorExtractor() { + list.add(new Channel(R.string.channel_black, 0, MAX_VALUE, new Channel.ColorExtractor() { @Override public int extract(int color) { - return 100 - (int) (Color.alpha((int) (color * 2.55)) / 2.55); + return (int) black(color); } })); return list; } + // K = 1-max(R', G', B') + private double black(int color) { + return MAX_VALUE - Math.max(Math.max(Color.red(color)/fraction(), Color.green(color)/fraction()), Color.blue(color)/fraction()); + } + @Override public int evaluateColor(List channels) { return Color.rgb( @@ -57,6 +75,6 @@ public int evaluateColor(List channels) { } private int convertToRGB(Channel colorChan, Channel blackChan) { - return (int)((255 - colorChan.getProgress() * 2.55) * (255 - blackChan.getProgress() * 2.55)) / 255; + return (int)((255 - colorChan.getProgress() * fraction()) * (255 - blackChan.getProgress() * fraction())) / 255; } } diff --git a/library/src/main/java/com/kunzisoft/androidclearchroma/colormode/mode/CMYK255.java b/library/src/main/java/com/kunzisoft/androidclearchroma/colormode/mode/CMYK255.java index 0b6fb7d..f7e3169 100644 --- a/library/src/main/java/com/kunzisoft/androidclearchroma/colormode/mode/CMYK255.java +++ b/library/src/main/java/com/kunzisoft/androidclearchroma/colormode/mode/CMYK255.java @@ -1,62 +1,11 @@ package com.kunzisoft.androidclearchroma.colormode.mode; -import android.graphics.Color; - -import com.kunzisoft.androidclearchroma.R; -import com.kunzisoft.androidclearchroma.colormode.Channel; - -import java.util.ArrayList; -import java.util.List; - /** * Created by Pavel Sikun on 01.04.16. */ -public class CMYK255 implements AbstractColorMode { - - @Override - public List getChannels() { - List list = new ArrayList<>(); - - list.add(new Channel(R.string.channel_cyan, 0, 255, new Channel.ColorExtractor() { - @Override - public int extract(int color) { - return 255 - Color.red(color); - } - })); - - list.add(new Channel(R.string.channel_magenta, 0, 255, new Channel.ColorExtractor() { - @Override - public int extract(int color) { - return 255 - Color.green(color); - } - })); - - list.add(new Channel(R.string.channel_yellow, 0, 255, new Channel.ColorExtractor() { - @Override - public int extract(int color) { - return 255 - Color.blue(color); - } - })); - - list.add(new Channel(R.string.channel_black, 0, 255, new Channel.ColorExtractor() { - @Override - public int extract(int color) { - return 255 - Color.alpha(color); - } - })); - - return list; - } - - @Override - public int evaluateColor(List channels) { - return Color.rgb( - convertToRGB(channels.get(0), channels.get(3)), - convertToRGB(channels.get(1), channels.get(3)), - convertToRGB(channels.get(2), channels.get(3))); - } +public class CMYK255 extends CMYK { - private int convertToRGB(Channel colorChan, Channel blackChan) { - return ((255 - colorChan.getProgress()) * (255 - blackChan.getProgress())) / 255; + public CMYK255() { + MAX_VALUE = 255; } } diff --git a/library/src/main/java/com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.java b/library/src/main/java/com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.java index b37abc2..2df5fce 100644 --- a/library/src/main/java/com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.java +++ b/library/src/main/java/com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.java @@ -18,7 +18,6 @@ import com.kunzisoft.androidclearchroma.colormode.Channel; import com.kunzisoft.androidclearchroma.colormode.ColorMode; import com.kunzisoft.androidclearchroma.listener.OnColorChangedListener; -import com.kunzisoft.androidclearchroma.listener.OnColorSelectedListener; import com.kunzisoft.androidclearchroma.view.ChannelView; import java.util.ArrayList; @@ -39,9 +38,7 @@ public class ChromaColorFragment extends Fragment { public final static String ARG_COLOR_MODE = "arg_color_mode"; public final static String ARG_INDICATOR_MODE = "arg_indicator_mode"; - private - @ColorInt - int currentColor; + private @ColorInt int currentColor; private ColorMode colorMode; private IndicatorMode indicatorMode; diff --git a/library/src/main/java/com/kunzisoft/androidclearchroma/view/ChannelView.java b/library/src/main/java/com/kunzisoft/androidclearchroma/view/ChannelView.java index 14f4aa3..fa2103c 100644 --- a/library/src/main/java/com/kunzisoft/androidclearchroma/view/ChannelView.java +++ b/library/src/main/java/com/kunzisoft/androidclearchroma/view/ChannelView.java @@ -36,7 +36,8 @@ public ChannelView(Channel channel, @ColorInt int color, IndicatorMode indicator channel.setProgress(channel.getExtractor().extract(color)); if(channel.getProgress() < channel.getMin() || channel.getProgress() > channel.getMax()) { throw new IllegalArgumentException( - "Initial progress for channel: " + channel.getClass().getSimpleName() + "Initial progress " + channel.getProgress() + + " for channel: " + channel.getClass().getSimpleName() + " must be between " + channel.getMin() + " and " + channel.getMax()); } diff --git a/sample/src/main/java/com/kunzisoft/androidclearchroma/sample/MainActivity.java b/sample/src/main/java/com/kunzisoft/androidclearchroma/sample/MainActivity.java index d07e35e..58e7476 100644 --- a/sample/src/main/java/com/kunzisoft/androidclearchroma/sample/MainActivity.java +++ b/sample/src/main/java/com/kunzisoft/androidclearchroma/sample/MainActivity.java @@ -112,6 +112,11 @@ private void setupSpinner() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { colorMode = ColorMode.values()[position]; + switch(colorMode) { + case CMYK: + case CMYK255: + indicatorMode = IndicatorMode.DECIMAL; + } chromaDialog = null; }