Click here to Skip to main content
15,892,768 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 2 – Southwind Logic

Rate me:
Please Sign up or sign in to vote.
4.45/5 (6 votes)
15 Nov 2012LGPL325 min read 31.5K   1K   22  
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
//
// SupportingClasses.cs 
//
// 
using System;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Ink;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Input;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Controls.Primitives;

namespace Signum.Windows.ColorUtils
{
 
    internal struct HsvColor
    {
        public double H;
        public double S;
        public double V;
        public HsvColor(double h, double s, double v)
        {
            this.H = h;
            this.S = s;
            this.V = v;
        }

        public static HsvColor FromColor(Color color)
        {
            return FromRGB(color.R, color.G, color.B);
        }

        public static HsvColor FromRGB(int r, int g, int b)
        {
            double h;
            double min = Math.Min(Math.Min(r, g), b);
            double v = Math.Max(Math.Max(r, g), b);
            double delta = v - min;
            double s = (v == 0.0) ? 0.0 : (delta / v);
            if (s == 0.0)
            {
                h = 0.0;
            }
            else
            {
                if (r == v)
                {
                    h = ((double)(g - b)) / delta;
                }
                else if (g == v)
                {
                    h = 2.0 + (((double)(b - r)) / delta);
                }
                else
                {
                    h = 4.0 + (((double)(r - g)) / delta);
                }
                h *= 60.0;
                if (h < 0.0)
                {
                    h += 360.0;
                }
            }
            return new HsvColor(h, s, v / 255.0);
        }

        public Color ToColor()
        {
            return this.ToColor(255);
        }

        public Color ToColor(byte alpha)
        {
            double h = this.H;
            double s = this.S;
            double v = this.V;
            double r,g,b;
            if (s == 0.0)
            {
                r = v; g = v; b = v;
            }
            else
            {
                if (h == 360.0)
                {
                    h = 0.0;
                }
                else
                {
                    h /= 60.0;
                }
                int i = (int)Math.Truncate(h);
                double f = h - i;
                double p = v * (1.0 - s);
                double q = v * (1.0 - (s * f));
                double t = v * (1.0 - (s * (1.0 - f)));
                switch (i)
                {
                    case 0: r = v; g = t; b = p; break;
                    case 1: r = q; g = v; b = p; break;
                    case 2: r = p; g = v; b = t; break;
                    case 3: r = p; g = q; b = v; break;
                    case 4: r = t; g = p; b = v; break;
                    default: r = v; g = p; b = q; break;
                }
            }

            return Color.FromArgb(alpha, (byte)(r * 255.0), (byte)(g * 255.0), (byte)(b * 255.0));
        }
    }

 





}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Signum Software
Spain Spain
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.

www.signumframework.com

I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...

Comments and Discussions