小项目分享---混色器
编写代码的同志们一般懂美术的就少了,偶也是,什么色轮、三维加色等等。虽然看过一些书籍(如内田广由纪的《配色基础原理》),不过还是一知半解的。
下面这些专业配色工具一直也不怎么会用。
所以,本着程序为生活服务的原则,我用java和c#分别编写了一个混色器。想看看不同比率的颜色混合起来得到的是什么效果么?代码如下,与君分享:
1、Java Applet:
package edu.sx.william;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
public class SetBakColor extends JApplet implements MouseMotionListener {
private static final long serialVersionUID = 1L;
JScrollBar jsb, jsb2, jsb3;
JLabel jl;
Container con;
public void init() {
Panel pan = new Panel();
pan.setLayout(new GridLayout(2, 3));
con = getContentPane();
con.setLayout(new BorderLayout());
jsb = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 255);
jsb2 = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 255);
jsb3 = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 255);
jsb.addMouseMotionListener(this);
jsb2.addMouseMotionListener(this);
jsb3.addMouseMotionListener(this);
jl = new JLabel("the RGB color");
jl.addMouseMotionListener(this);
pan.add(jsb);
pan.add(jsb2);
pan.add(jsb3);
pan.add(jl);
con.add(pan,BorderLayout.NORTH);
}
@Override
public void mouseDragged(MouseEvent e) {
jl.setText("R:" + jsb.getValue() + " G:" + jsb2.getValue() + " B:"
+ jsb3.getValue());
con.setBackground(new Color(jsb.getValue(), jsb2.getValue(), jsb3
.getValue()));
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
运行效果:
2、C# WinForm
(Winform只列出了功能实现代码,用到的控件为GroupBox、Label、HScrollBar)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WfAdo
{
public partial class SetBakColor : Form
{
public SetBakColor()
{
InitializeComponent();
}
int r = 0, g = 0, b = 0;
private void hScrollBar3_Scroll(object sender, ScrollEventArgs e)
{
label3.Text = hScrollBar3.Value.ToString();
r = int.Parse(label3.Text);
this.BackColor = Color.FromArgb(r, g, b);
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
label1.Text = hScrollBar1.Value.ToString();
g = hScrollBar1.Value;//两种获得颜色的方法!
this.BackColor = Color.FromArgb(r, g, b);
}
private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)
{
label2.Text = hScrollBar2.Value.ToString();
b = int.Parse(label2.Text);
this.BackColor = Color.FromArgb(r, g, b);
}
}
}//好像C#敲的代码更少些:-)
运行效果: