zoukankan      html  css  js  c++  java
  • 简易调色盘控件 for .NET(EN)

    By Conmajia
    Originally posted in 2012

    Introduction

    Simple & fast implementation of a rectangular RGB palette control for .NET Fx 2.0. Old-school shit.

    Name it as SRP.

    Project download… Nah...

    (ancient project, where on earth can I retrieve those antiques?)

    The palette looks like:

    ▲ The look of SRP

    Features

    • Pick a color

      You can access the color you pick via e.Color of the ColorChanged event.

    • Set palette block size

      Note that size of the whole palette will be changed.

    Screenshots

    Here I have several examples. SRP is formed in a 6×36 color grid.

    ▲ Block 5
    ▲ Block 10
    ▲ Block 15

    Layers

    Disassemble SRP into layers for graphic painting. (bottom to top)

    1. Canvas
    2. Color blocks
    3. Grids
    4. Border
    5. Cursor

    Paint It

    Paint these layer sequentially.

    protected override void OnPaint(PaintEventArgs e) {
      Graphics g = e.Graphics;
      drawPalette(g);
      drawGrid(g);
      drawBorder(g);
      drawCursor(g);
    }
    

    A hello to the modern Flat UI 7 yrs. ago.

    void drawGrid(Graphics g) {
      for(int i = 0; i < rows; i++) {
        g.DrawLine(Pens.Black, 0, blockWidth * (i + 1), blockWidth * cols, blockWidth * (i + 1));
      }
      for(int i = 0; i < cols; i++) {
        g.DrawLine(Pens.Black, blockWidth * (i + 1), 0, blockWidth * (i + 1), blockWidth * rows);
      }
    }
    

    Calculate coordinates of a color, fill blocks, proceed on. Here’s my algorithm of generating RGB colors, you can generate your own shits.

    Color getColor(int row, int col) {
      byte r = 0, g = 0, b = 0;
      int step = 0xff / (rows - 1);
      r = (byte)(row * step);
      g = (byte)(step * (col / rows));
      b = (byte)(step * (col % rows));
      return Color.FromArgb(r, g, b);
    }
    

    Instead of storing preset colors, all colors shown were automatically generated during run-time.

    current = getColor(pt.Y / blockWidth, pt.X / blockWidth);
    

    Draw mouse cursor. Refresh only dirty parts on canvas.

    void updateCursor(Point pt) {
      lastCursor.X = cursor.X;
      lastCursor.Y = cursor.Y;
      cursor.X = pt.X - pt.X % blockWidth;
      cursor.Y = pt.Y - pt.Y % blockWidth;
      current = getColor(pt.Y / blockWidth, pt.X / blockWidth);
    }
    

    Redraw dirty.

    protected override void OnMouseMove(MouseEventArgs e) {
      updateCursor(e.Location);
      // redraw larger spaces
      Invalidate(new Rectangle(lastCursor.X - 1, lastCursor.Y - 1, lastCursor.Width + 2, lastCursor.Height + 2));
      Invalidate(new Rectangle(cursor.X - 1, cursor.Y - 1, cursor.Width + 2, cursor.Height + 2));
      // fire event
      OnColorChanged();
    }
    

    Trigger OnColorChanged() event which happens after color is changed.

    // -- custom events
    public delegate void ColorChangedEventHandler(object sender, ColorChangedEventArgs e);
    [Description("Fires every time when color changed.")]
    public event ColorChangedEventHandler ColorChanged;
    protected virtual void OnColorChanged() {
      if(ColorChanged != null) ColorChanged(this, new ColorChangedEventArgs(current));
    }
    // custom event args
    public class ColorChangedEventArgs: EventArgs {
      Color color = Color.Black;
      public Color Color {
        get {
          return color;
        }
        set {
          color = value;
        }
      }
      public ColorChangedEventArgs(Color color): base() {
        this.color = color;
      }
    }
    

    Application

    With some extra optimizations, you'll have one simple yet elegant palette.

    ▲ SRP in use

    The End. (Box)

  • 相关阅读:
    Django测试开发-20-settings.py中templates配置,使得APP下的模板以及根目录下的模板均可生效
    Django测试开发-19-auth模块之session,cookie
    Django测试开发-19-引入xadmin
    Django测试开发-17-报错:No module named 'django.contrib.staticfiles.templatetags'
    Django测试开发-16-ImportError: cannot import name 'six' from 'django.utils'
    Django测试开发-15-django.utils.encoding未发现 python_2_unicode_compatible包
    Django测试开发-14-数据库表设计:多对多,一对一,一对多
    Django测试开发-13-优化表单提交(GET、POST、登录、注册)
    Django测试开发-12-优化admin (2020-03-13 18:57)
    Django测试开发-11-返回json数据
  • 原文地址:https://www.cnblogs.com/conmajia/p/simple-palette-control.html
Copyright © 2011-2022 走看看