簡體   English   中英

每個像素的顏色混合

[英]Color blending per pixel

我想了解圖形。 現在我陷入色彩混合。 我試圖使用一些基本算法來合並rgb中的顏色。 現在我想像現實生活中那樣合並它們,即黃色+藍色=綠色。 我有這樣的方法。

package com.boxonix.light.utils;

public class Utils {

   public static int rgbToHex(int r, int g, int b) {
      return (1048576 * r) + (255 * g) + b;
   }

   public static int blendPixels(int r, int g, int b, double alpha, int bgPixel){   
      return 0;
   }

   public static int getRed(int color) {
      int red = Math.floorDiv(color, 1048576);
      return red;
   }

   public static int getGreen(int color) {
      int green = Math.floorDiv(color % 65536, 256);
      return green;
   }

   public static int getBlue(int color) {
      int blue = Math.floorDiv(color % 1048576, 256);
      return blue;
   }
}

r,g,b是顏色代表越過背景像素的像素(bgPix)alpha是透明度(0.0 - 1.0)。 我可以將bgPix轉換為r1,g1,b1。 現在我需要混合它們,幫助! :d

聽起來你正試圖將前景色混合在一些背景色上。 令人高興的是,它很簡單:

alpha * foregroundColor + (1 - alpha) * backgroundColor

這個想法是只有部分顏色來自前景色,其余的( 1 - alpha )來自背景。 如果你仔細想想,你可以直觀地看到它:如果alpha為0,那么所有顏色都來自背景; 如果alpha為1,則所有都來自前景。

使用上面的方法定義的示例代碼:

public static int blendPixels(int r, int g, int b, double alpha, int bgPixel) {   
   int blendedRed = (int)Math.round(alpha * r + (1.0 - alpha) * getRed(bgPixel));
   int blendedGreen = (int)Math.round(alpha * g + (1.0 - alpha) * getGreen(bgPixel));
   int blendedBlue = (int)Math.round(alpha * b + (1.0 - alpha) * getBlue(bgPixel));
   return rgbToHex(blendedRed, blendedGreen, blendedBlue);
}

希望有所幫助!

實際上並不是答案,但這里是根據公式快速實現每個像素的顏色混合

alpha * foregroundColor +(1 - alpha)* backgroundColor

這似乎是錯誤的 - 看截圖 - 並隨意使用代碼進行測試。

截圖:

像素混合做錯了

碼:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Locale;
import java.util.Random;

public class Program {
    static JFrame mainFrame;
    static JPanel view;
    static JLabel fgLabel;
    static JSlider fgTrack;
    static JLabel alphaLabel;
    static JSlider alphaTrack;
    static JLabel bgLabel;
    static JSlider bgTrack;

    static int fg;
    static float alpha;
    static int blend;
    static int bg;

    public static void main(String[] args) {
        //setup application
        mainFrame = new JFrame() {{
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setContentPane(new JPanel() {{
                setLayout(new BorderLayout());
                setSize(640, 480);
                setPreferredSize(getSize());
                add(new JPanel() {{
                    setLayout(new FlowLayout());
                    add(fgLabel = new JLabel());
                    add(fgTrack = new JSlider(0, 100) {{
                        setOrientation(JSlider.VERTICAL);
                        setMajorTickSpacing(25);
                        setMinorTickSpacing(5);
                        setPaintTrack(true);
                        setPaintTicks(true);
                        addChangeListener(e -> {
                            fg = Color.getHSBColor(getValue() / 100f, 1f, 1f).getRGB();
                            fgLabel.setText("fg=" + rgbToString(fg));
                            view.repaint();
                        });
                    }});
                    add(alphaLabel = new JLabel());
                    add(alphaTrack = new JSlider(0, 100) {{
                        setOrientation(JSlider.VERTICAL);
                        setMajorTickSpacing(25);
                        setMinorTickSpacing(5);
                        setPaintTrack(true);
                        setPaintTicks(true);
                        addChangeListener(e -> {
                            alpha = getValue() / 100f;
                            blend = blendPixels(fg, alpha, bg);
                            alphaLabel.setText("<html>alpha=" + alpha+"<br>blend=" + rgbToString(blend));
                            view.repaint();
                        });
                    }});
                    add(bgLabel = new JLabel());
                    add(bgTrack = new JSlider(0, 100) {{
                        setOrientation(JSlider.VERTICAL);
                        setMajorTickSpacing(25);
                        setMinorTickSpacing(5);
                        setPaintTrack(true);
                        setPaintTicks(true);
                        addChangeListener(e -> {
                            bg = Color.getHSBColor(getValue() / 100f, 1f, 1f).getRGB();
                            bgLabel.setText("bg=" + rgbToString(bg));
                            view.repaint();
                        });
                    }});
                }}, BorderLayout.NORTH);
                add(view = new JPanel() {
                    @Override
                    public void paintComponent(Graphics g) {
                        super.paintComponent(g);

                        Graphics2D g2 = (Graphics2D) g;
                        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                        //"transparent" background
                        final int m = 8;
                        for (int r = 0, x = 0; x < getWidth(); r++, x += m) {
                            for (int c = 0, y = 0; y < getHeight(); c++, y += m) {
                                g2.setColor((r + c) % 2 == 0 ? Color.WHITE : Color.LIGHT_GRAY);
                                g2.fillRect(x, y, m, m);
                            }
                        }

                        //circles
                        final int d = Math.min(getWidth() / 2, getHeight());
                        g2.setColor(new Color(bg));
                        g2.fillOval(d, 0, d, d);
                        g2.setColor(new Color(blend));
                        g2.fillOval(d / 2, 0, d, d);
                        g2.setColor(new Color(fg));
                        g2.fillOval(0, 0, d, d);
                    }
                }, BorderLayout.CENTER);
            }});
        }};

        //randomize values
        Random rng = new Random();
        fgTrack.setValue(rng.nextInt(100));
        alphaTrack.setValue(rng.nextInt(100));
        bgTrack.setValue(rng.nextInt(100));

        //display
        mainFrame.pack();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
    }

    public static String rgbToString(int rgb) {
        return Integer.toHexString(rgb).toUpperCase(Locale.ROOT);
    }

    public static int rgbToHex(int r, int g, int b) {
        return (1 << 16) * r
                + (1 << 8) * g
                + (1 << 0) * b;
    }

    public static int blendPixels(int fgPixel, float alpha, int bgPixel) {
        return rgbToHex(
                blendPixelComponent(getRed(fgPixel), alpha, getRed(bgPixel)),
                blendPixelComponent(getGreen(fgPixel), alpha, getGreen(bgPixel)),
                blendPixelComponent(getBlue(fgPixel), alpha, getBlue(bgPixel))
        );
    }

    public static int blendPixelComponent(int fgComp, float alpha, int bgComp) {
        final float beta = 1 - alpha;
        return Math.round(alpha * fgComp + beta * bgComp);
    }

    public static int getRed(int color) {
        return (color >> 16) & 0xFF;
    }

    public static int getGreen(int color) {
        return (color >> 8) & 0xFF;
    }

    public static int getBlue(int color) {
        return (color >> 0) & 0xFF;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM