Post by microfarad on Nov 28, 2010 3:57:31 GMT
I've gotten back into my old image editing programs... This time, I'm going over the top
The MicroChromaticWorkspace is a bunch of classes I'm writing for Visual Basic, to facilitate easy image awesomeness. It's poorly organized right now, and there are some critical functions missing. It will get better as time goes on. See my thread "HAH!" for examples of what you can do in Visual Basic using my classes...
The Doodler class is barely functional SOMETIMES. I thought it would improve the speed of VB graphics, but it gives me lots of errors and I'm not sure it helps...
The MicroChromaticWorkspace is a bunch of classes I'm writing for Visual Basic, to facilitate easy image awesomeness. It's poorly organized right now, and there are some critical functions missing. It will get better as time goes on. See my thread "HAH!" for examples of what you can do in Visual Basic using my classes...
The Doodler class is barely functional SOMETIMES. I thought it would improve the speed of VB graphics, but it gives me lots of errors and I'm not sure it helps...
Class MscFns
Shared Function RGBtoLUM(ByVal Frm As RGB) As Double
RGBtoLUM = (Frm.R + Frm.G + Frm.B) / 3
End Function
Shared Function RGBmix(ByVal A As RGB, ByVal B As RGB, ByVal Aw As Double, ByVal Bw As Double) As RGB
RGBmix = New RGB
RGBmix.R = (Aw * A.R) + (Bw * B.R)
RGBmix.G = (Aw * A.G) + (Bw * B.G)
RGBmix.B = (Aw * A.B) + (Bw * B.B)
End Function
Shared Function COLORtoRGB(ByVal Frm As Color) As RGB
COLORtoRGB = New RGB
COLORtoRGB.R = Frm.R / 255
COLORtoRGB.G = Frm.G / 255
COLORtoRGB.B = Frm.B / 255
End Function
Shared Function RGBtoCOLOR(ByVal Frm As RGB) As Color
Try
RGBtoCOLOR = Color.FromArgb(255, Frm.R * 255, Frm.G * 255, Frm.B * 255)
Catch
RGBtoCOLOR = Color.Lime
End Try
End Function
Shared Function HSLtoRGB(ByVal Frm As HSL) As RGB
Dim C As Double
Dim Hp As Double
Dim X As Double
Dim Rp As Double = 0
Dim Gp As Double = 0
Dim Bp As Double = 0
Dim m As Double
C = (1 - Math.Abs(2 * Frm.L - 1)) * Frm.S
Hp = 6 * (Frm.H Mod 1)
X = C * (1 - Math.Abs((Hp Mod 2) - 1))
If Hp < 1 Then
Rp = C
Gp = X
ElseIf Hp < 2 Then
Rp = X
Gp = C
ElseIf Hp < 3 Then
Gp = C
Bp = X
ElseIf Hp < 4 Then
Gp = X
Bp = C
ElseIf Hp < 5 Then
Rp = X
Bp = C
Else
Rp = C
Bp = X
End If
m = Frm.L - (C / 2)
HSLtoRGB = New RGB
HSLtoRGB.R = Rp + m
HSLtoRGB.G = Gp + m
HSLtoRGB.B = Bp + m
If Frm.L = 0 Then
HSLtoRGB.R = 0
HSLtoRGB.G = 0
HSLtoRGB.B = 0
End If
End Function
Shared Function RGBtoHSL(ByVal Frm As RGB) As HSL
Dim Vals As New List(Of Double)
Dim C As Double
Vals.Add(Frm.R)
Vals.Add(Frm.G)
Vals.Add(Frm.B)
C = Vals.Max() - Vals.Min()
RGBtoHSL = New HSL
If C = 0 Then
RGBtoHSL.H = 0
ElseIf Vals.Max() = Frm.R Then
RGBtoHSL.H = ((Frm.G - Frm.B) / C) Mod 6
ElseIf Vals.Max() = Frm.G Then
RGBtoHSL.H = ((Frm.B - Frm.R) / C) + 2
ElseIf Vals.Max() = Frm.B Then
RGBtoHSL.H = ((Frm.R - Frm.G) / C) + 4
End If
RGBtoHSL.H = RGBtoHSL.H / 6
RGBtoHSL.L = (Vals.Max + Vals.Min) / 2
If ((Frm.R = 0) And (Frm.G = 0) And (Frm.B = 0)) Then
RGBtoHSL.L = 0
End If
If C = 0 Then
RGBtoHSL.S = RGBtoHSL2(New RGB(Frm.R, Frm.G, Frm.B * 0.9)).S
Else
RGBtoHSL.S = C / (1 - Math.Abs((2 * RGBtoHSL.L) - 1))
End If
End Function
Shared Function RGBtoHSL2(ByVal Frm As RGB) As HSL
Dim Vals As New List(Of Double)
Dim C As Double
Vals.Add(Frm.R)
Vals.Add(Frm.G)
Vals.Add(Frm.B)
C = Vals.Max() - Vals.Min()
RGBtoHSL2 = New HSL
If C = 0 Then
RGBtoHSL2.H = 0
ElseIf Vals.Max() = Frm.R Then
RGBtoHSL2.H = ((Frm.G - Frm.B) / C) Mod 6
ElseIf Vals.Max() = Frm.G Then
RGBtoHSL2.H = ((Frm.B - Frm.R) / C) + 2
ElseIf Vals.Max() = Frm.B Then
RGBtoHSL2.H = ((Frm.R - Frm.G) / C) + 4
End If
RGBtoHSL2.H = RGBtoHSL2.H / 6
RGBtoHSL2.L = (Vals.Max + Vals.Min) / 2
If ((Frm.R = 0) And (Frm.G = 0) And (Frm.B = 0)) Then
RGBtoHSL2.L = 0
End If
If C = 0 Then
RGBtoHSL2.S = 0
Else
RGBtoHSL2.S = C / (1 - Math.Abs((2 * RGBtoHSL2.L) - 1))
End If
End Function
End Class
Class HSL
Public H As Double
Public S As Double
Public L As Double
Sub New()
End Sub
Sub New(ByVal H As Double, ByVal S As Double, ByVal L As Double)
Me.H = H
Me.S = S
Me.L = L
End Sub
End Class
Class RGB
Public R As Double
Public G As Double
Public B As Double
Sub New()
End Sub
Sub New(ByVal R As Double, ByVal B As Double, ByVal G As Double)
Me.R = R
Me.B = B
Me.G = G
End Sub
End Class
Class Doodler
Dim bmp As Bitmap
Dim stream As New IO.MemoryStream
Dim start As Integer
Dim pad As Integer
Public Property Canvas() As Bitmap
Get
bmp = Image.FromStream(stream)
Return bmp.Clone
End Get
Set(ByVal value As Bitmap)
Dim gr As Graphics
bmp = New Bitmap(value.Width, value.Height, Imaging.PixelFormat.Format24bppRgb)
gr = Graphics.FromImage(bmp)
gr.DrawImage(value, 0, 0)
bmp.Save(stream, Imaging.ImageFormat.Bmp)
pad = 4 - ((bmp.Width * 3) Mod 4)
If pad = 4 Then
pad = 0
End If
start = stream.Length - (((bmp.Width * 3) + pad) * bmp.Height)
End Set
End Property
Public Sub SetPixel(ByVal X As Integer, ByVal Y As Integer, ByVal Col As Color)
stream.Seek(54 + (X * 3) + (((3 * bmp.Width) + pad) * (bmp.Height - Y - 1)), IO.SeekOrigin.Begin)
stream.WriteByte(Col.B)
stream.WriteByte(Col.G)
stream.WriteByte(Col.R)
End Sub
Public Function GetPixel(ByVal X As Integer, ByVal Y As Integer) As Color
Dim R As Byte
Dim G As Byte
Dim B As Byte
stream.Seek(54 + (X * 3) + (((3 * bmp.Width) + pad) * (bmp.Height - Y - 1)), IO.SeekOrigin.Begin)
B = stream.ReadByte()
G = stream.ReadByte()
R = stream.ReadByte()
GetPixel = Color.FromArgb(255, R, B, G)
End Function
End Class
Class Vector3
Public X As Double
Public Y As Double
Public Z As Double
Sub New()
End Sub
Sub New(ByVal X As Double, ByVal Y As Double, ByVal Z As Double)
Me.X = X
Me.Y = Y
Me.Z = Z
End Sub
Public Function Clone() As Vector3
Clone = New Vector3(Me.X, Me.Y, Me.Z)
End Function
Shared Function Add(ByVal A As Vector3, ByVal B As Vector3) As Vector3
Add = New Vector3(A.X + B.X, A.Y + B.Y, A.Z + B.Z)
End Function
Shared Function Sbt(ByVal A As Vector3, ByVal B As Vector3) As Vector3
Sbt = New Vector3(A.X - B.X, A.Y - B.Y, A.Z - B.Z)
End Function
Shared Function Scl(ByVal A As Vector3, ByVal N As Double) As Vector3
Scl = New Vector3(A.X * N, A.Y * N, A.Z * N)
End Function
Shared Function Mag(ByVal A As Vector3) As Double
Mag = ((A.X ^ 2) + (A.Y ^ 2) + (A.Z ^ 2)) ^ 0.5
End Function
Shared Function Unt(ByVal A As Vector3) As Vector3
Unt = Vector3.Scl(A, 1 / Vector3.Mag(A))
End Function
End Class
Class Vector2
Public X As Double
Public Y As Double
Sub New()
End Sub
Sub New(ByVal X As Double, ByVal Y As Double)
Me.X = X
Me.Y = Y
End Sub
Public Function Clone() As Vector2
Clone = New Vector2(Me.X, Me.Y)
End Function
Shared Function Add(ByVal A As Vector2, ByVal B As Vector2) As Vector2
Add = New Vector2(A.X + B.X, A.Y + B.Y)
End Function
Shared Function Sbt(ByVal A As Vector2, ByVal B As Vector2) As Vector2
Sbt = New Vector2(A.X - B.X, A.Y - B.Y)
End Function
Shared Function Scl(ByVal A As Vector2, ByVal N As Double) As Vector2
Scl = New Vector2(A.X * N, A.Y * N)
End Function
Shared Function Mag(ByVal A As Vector2) As Double
Mag = ((A.X ^ 2) + (A.Y ^ 2)) ^ 0.5
End Function
Shared Function Unt(ByVal A As Vector2) As Vector2
Unt = Vector2.Scl(A, 1 / Vector2.Mag(A))
End Function
End Class
Class Bezier2
Dim Pts As List(Of Vector2)
Sub New()
Pts = New List(Of Vector2)
End Sub
Public Function Clone() As Bezier2
Clone = New Bezier2
Clone.Pts = New List(Of Vector2)
For n = 0 To Me.Pts.Count - 1
Clone.Pts.Add(Me.Pts(n).Clone)
Next
End Function
Public Property Points() As List(Of Vector2)
Get
Points = Pts
End Get
Set(ByVal value As List(Of Vector2))
Pts = Points
End Set
End Property
Shared Function Eval(ByVal t As Double, ByVal Curve As Bezier2) As Vector2
Eval = New Vector2
For i = 0 To Curve.Points.Count - 1
Eval = Vector2.Add(Eval, Vector2.Scl(Curve.Points(i), nCk(Curve.Points.Count - 1, i) * ((1 - t) ^ (Curve.Points.Count - 1 - i)) * (t ^ i)))
Next
End Function
Shared Function Tangent(ByVal t As Double, ByVal Curve As Bezier2) As Bezier2
Dim front As Bezier2 = Curve.Clone
front.Pts.RemoveAt(0)
Tangent = New Bezier2
Tangent.Pts.Add(Bezier2.Eval(t, Curve))
Tangent.Pts.Add(Vector2.Add(Bezier2.Eval(t, Curve), Vector2.Unt(Vector2.Sbt(Bezier2.Eval(t, front), Bezier2.Eval(t, Curve)))))
End Function
Shared Function Chord(ByVal t1 As Double, ByVal t2 As Double, ByVal Curve As Bezier2) As Bezier2
Chord = New Bezier2
Chord.Pts.Add(Bezier2.Eval(t1, Curve))
Chord.Pts.Add(Bezier2.Eval(t2, Curve))
End Function
Shared Function nCk(ByVal n As Integer, ByVal k As Integer) As Integer
nCk = 1
For i = 1 To n
nCk = nCk * i
Next
For i = 1 To n - k
nCk = nCk / i
Next
For i = 1 To k
nCk = nCk / i
Next
End Function
End Class
Class Bezier3
Dim Pts As List(Of Vector3)
Public Function Clone() As Bezier3
Clone = New Bezier3
Clone.Pts = New List(Of Vector3)
For n = 0 To Me.Pts.Count - 1
Clone.Pts.Add(Me.Pts(n).Clone)
Next
End Function
Sub New()
Pts = New List(Of Vector3)
End Sub
Public Property Points() As List(Of Vector3)
Get
Points = Pts
End Get
Set(ByVal value As List(Of Vector3))
Pts = Points
End Set
End Property
Shared Function Eval(ByVal t As Double, ByVal Curve As Bezier3) As Vector3
Eval = New Vector3
For i = 0 To Curve.Points.Count - 1
Eval = Vector3.Add(Eval, Vector3.Scl(Curve.Points(i), nCk(Curve.Points.Count - 1, i) * ((1 - t) ^ (Curve.Points.Count - 1 - i)) * (t ^ i)))
Next
End Function
Shared Function Tangent(ByVal t As Double, ByVal Curve As Bezier3) As Bezier3
Dim front As Bezier3 = Curve.Clone
front.Pts.RemoveAt(0)
Tangent = New Bezier3
Tangent.Pts.Add(Bezier3.Eval(t, Curve))
Tangent.Pts.Add(Vector3.Add(Bezier3.Eval(t, Curve), Vector3.Unt(Vector3.Sbt(Bezier3.Eval(t, front), Bezier3.Eval(t, Curve)))))
End Function
Shared Function Chord(ByVal t1 As Double, ByVal t2 As Double, ByVal Curve As Bezier3) As Bezier3
Chord = New Bezier3
Chord.Pts.Add(Bezier3.Eval(t1, Curve))
Chord.Pts.Add(Bezier3.Eval(t2, Curve))
End Function
Shared Function nCk(ByVal n As Integer, ByVal k As Integer) As Integer
nCk = 1
For i = 1 To n
nCk = nCk * i
Next
For i = 1 To n - k
nCk = nCk / i
Next
For i = 1 To k
nCk = nCk / i
Next
End Function
End Class
Class Complex
Public a As Double
Public b As Double
Sub New()
End Sub
Sub New(ByVal a As Double, ByVal b As Double)
Me.a = a
Me.b = b
End Sub
Sub New(ByVal z As Complex)
Me.a = z.a
Me.b = z.b
End Sub
Public Function clone() As Complex
clone = New Complex(Me)
End Function
Shared Function Mul(ByVal A As Complex, ByVal B As Complex) As Complex
Mul = New Complex()
Mul.a = (A.a * B.a) - (A.b * B.b)
Mul.b = (A.a * B.b) + (A.b * B.a)
End Function
Shared Function Div(ByVal A As Complex, ByVal B As Complex) As Complex
Div = New Complex()
Div.a = (A.a / B.a) - (A.b / B.b)
Div.b = (A.a / B.b) + (A.b / B.a)
End Function
Shared Function Add(ByVal A As Complex, ByVal B As Complex) As Complex
Add = New Complex()
Add.a = A.a + B.a
Add.b = A.b + B.b
End Function
Shared Function Sbt(ByVal A As Complex, ByVal B As Complex) As Complex
Sbt = New Complex()
Sbt.a = A.a - B.a
Sbt.b = A.b - B.b
End Function
Shared Function Scl(ByVal A As Complex, ByVal B As Double) As Complex
Scl = New Complex()
Scl.a = A.a * B
Scl.b = A.b * B
End Function
Shared Function ToVector2(ByVal A As Complex) As Vector2
ToVector2 = New Vector2(A.a, A.b)
End Function
End Class