Euler angles, Quaternion and mobile device rotation I've written a JS SDK that listens to mobile device rotation, providing $3$ inputs:  
$\alpha$ : An angle can range between $0$ and $360$ degrees
$\beta$ : An Angle between $-180$ and $180$ degrees
$\gamma$ : An Angle between $-90$ to $90$ degrees
Documentation for device rotation
I have tried using Euler Angles to determine the device orientation but encountered the gimbal lock effect, that made calculation explode when the device was pointing up. That lead me to use Quaternion, that does not suffer from the gimbal effect.
I've found a library that converts $\alpha, \beta$ and $\gamma$ to a Quaternion, so for the following values:  
$\alpha : 40.3476$
$\beta : 70.3120$
$\gamma : -62.9454$ 
I get this Quaternion ($ZXY$ order):  
$w: 0.7582073853451148$
$x: 0.608178427083661$
$y: -0.23130444560993935$
$z: -0.04169910165995308$
Visualizing the device orientation (Demo, use mobile):

I would like to write a method that gets a Quaternion as an input and outputs if the device is on portrait or landscape mode.  
Defining portrait and landscape as a range of $\pm 45^\circ$ around the relevant axes. 
What approach should I take?
Update:
To elaborate further, my goals are:
To write a method that gets $\alpha, \beta$ and $\gamma$ and outputs if the device is in one of the following orientations:


*

*portrait

*portrait upside down

*landscape left

*landscape right

*display up

*display down

 A: Suppose the phone is in a global coordinate system with the origin located at the center of mass of the phone, the positive $Y$ axis in the direction of the longest side of the phone, the $X$ axis in the direction of the phone's screen width and the $Z$ axis in the direction of the screen's normal vector.
So, the identity rotation gives the portrait orientation of the phone in the global coord. system. 
Note that a single rotation of $\pi/2$ around the $Z$ axis give us the landscape orientation. If we decompose the phone orientation in two rotations, one about $Z$ axis and other about one of the other axis, I think that looking at the $Z$ axis rotation angle we could discover the phone's orientation.
A rotation can be factorized in two rotations around two orthogonal axis, in our case we are interested in factorize phone rotation in two rotations around $ZY$ and $ZX$. 
Let us define three axis vectors:
$$E_1 = (1, 0, 0)$$
$$E_2 = (0, 1, 0)$$
$$E_3 = (0, 0, 1)$$
Given a unit quaternion $Q$ the goal is to deconpose it in two rotations rotating an angle $a$ around axis $E_2$ and an angle $b$ around axis $E_3$:
$$Q = \exp(a E_2) \exp(b E_3)$$
Multiplying both sides by $E_3 Q^*$
$$Q E_3 Q^* = \exp(a E_2) \exp(b E_3) E_3 Q^*$$
Taking into account that $Q^* = \exp(b E_3)^* \exp(a E_2)^*$
$$Q E_3 Q^* = \exp(a E_2) \exp(b E_3)  E_3  \exp(b E_3)^* \exp(a E_2)^*$$
Noting that $E_3$ is an eigenvector of $\exp(b E_3)$ such that $E_3 = \exp(b E_3)  E_3  \exp(b E_3)^*$, we get:
$$W = \exp(a E_2) E_3 \exp(a E_2)^*$$
Where $W$ is the vector $E_3$ rotated by $Q$: $W = Q E_3 Q^*$
$$a = atan2( W \cdot E_1, W \cdot E_3)$$
Where $(\cdot)$ is the dot product.
Now, departing from the conjugate of $Q$:
$$Q^* = \exp(b E_3)^* \exp(a E_2)^*$$
Multiplying both sides by $E_2 Q$:
$$Q^* E_2 Q = \exp(b E_3)^* \exp(a E_2)^* E_2 Q$$
$$Q^* E_2 Q = \exp(-b E_3) \exp(-a E_2) E_2 \exp(a E_2) \exp(b E_3)$$
$$S = \exp(-b E_3) E_2 \exp(b E_3)$$
Where $S$ is the vector $E_2$ rotated by $Q^*$: $S = Q^* E_2 Q$
$$b = - atan2( S \cdot E_1, S \cdot E_2)$$
An analogous calculation can be done for factorizing $Q$ in two rotations around $E_1$ and $E_3$.
My guess is that having a measure of $b$ close enough to $\pm \pi/2$ then the phone is in landscape orientation. 
